search

Home  >  Q&A  >  body text

How to populate an HTML table from a design-generated database table

Very new to Ruby on Rails and devise. I'm currently trying to fill out a table on an HTML page that should list all the records from the Student table in my database (MYSQL).

<tbody>
<% for student in @students %>
 <tr>
  <td> <%= student.email %> </td>
 </tr>
<% end %>
</tbody>

I also tried @students.each |students|. Both variants give the error NoMethodError, undefined method 'each' for nil:NilClass According to the results of my online search, I am referencing the students table but not declaring it somewhere

def <name>
  @students = Students.all
end

As far as I know, it needs to go somewhere, but I don't know where.

Any suggestions on what to do would be great.

Rails 7.0.3 Design 4.8

P粉521697419P粉521697419227 days ago2586

reply all(1)I'll reply

  • P粉704066087

    P粉7040660872024-04-06 00:49:54

    You change Students to Student and use each to loop

    Create: users_controller.rb

    def index
     @students = Student.all
    end

    Create folder: views/users/index.html.erb

    @students.each do |student|
    // code
    end

    reply
    0
  • Cancelreply