ruby on rails - Model index.html page displaying duplicated data -
i'm getting strange behavior on model index page. when create 1 model object, displays correctly on index page. when create second model object, shows duplicates of both objects on index page, so
object object b object object b
i've confirmed duplicate objects not being created in database. also, when destroy object b, displays object correctly once.
index.html.erb
<table class="table"> <thead> <tr> <th>image</th> <th>name</th> <th>description</th> <th>url</th> <th></th> <th></th> <th></th> </tr> </thead> <tbody> <%= render @companies %> </tbody> </table>
_company.html.erb
<% @companies.each |company| %> <tr> <td><%= image_tag company.image(:medium) %></td> <td><%= company.name %></td> <td><%= company.description %></td> <td><%= company.url %></td> <td><%= link_to 'show', company %></td> <td><%= link_to 'edit', edit_company_path(company) %></td> <td><%= link_to 'destroy', company, method: :delete, data: { confirm: 'are sure?' } %></td> </tr> <% end %>
companies_controller.rb
def index @companies = company.all respond_to |format| format.html # index.html.erb format.json { render json: @companies } end end
change partial to,
<tr> <td><%= image_tag company.image(:medium) %></td> <td><%= company.name %></td> <td><%= company.description %></td> <td><%= company.url %></td> <td><%= link_to 'show', company %></td> <td><%= link_to 'edit', edit_company_path(company) %></td> <td><%= link_to 'destroy', company, method: :delete, data: { confirm: 'are sure?' } %></td> </tr>
you need drop each loop in partial.
the <%= render @companies %>
renders partial each company you're looping through companies again in each partial.
see 3.4.5 rendering collections @ http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections more info
Comments
Post a Comment