Div_for: NoMethodError

I tried the ror tutorial and I came across the following line of code:

index.html.erb:

<%= render :partial => @players %>

_player.html.erb:

<% div_for player do %>
<%= player.FNAME %> <%= player.SURNAME %>
<% end %>

players_controller.rb:

def index
    @players = Player.all(:order => "FNAME")
    respond_to do |format|
      format.html
    end
end

I want to change index.html.erb so that there is no need for partial, but it does not work properly.

Please see the code below.

index.html.erb:

<% div_for @players do %>
<%= @player.FNAME %> <%= @player.SURNAME %>
<% end %>

NoMethodError in player index #

+3
source share
3 answers

This is a direct translation of your code:

<% @players.each do |player| %>
  <% div_for player do %>
    <%= player.FNAME %> <%= player.SURNAME %>
  <% end %>
<% end %>

render :partial, given the collection ( @playersthis case), it will go through the collection one by one and display partial for you.

But rendering the collection also gives you a counter and separator pattern.

+3
source

Basically, div_for will look for an identifier to do:

 <div id="the_id">

Since you are passing an array, not an object anymore, it is lost.

content_tag .

. : http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag

+2
<div>
    <% @players.each do |player| %>
    <p><%= player.FNAME %></p>
    <p><%= player.SURNAME %></p>
    <% end %>
</div>

FYI is a good idea to keep this material partial.

+2
source

All Articles