Passing a local variable to a partial internal for each loop of loop 3

This is my code for rendering partial (the @parties assembly is created correctly, I tested it):

        <% @parties.each do |party| %>
            <div class="item">
              <%= render 'parties/party', :object => party  %>
            </div>
        <% end %>

And this is the code in partial:

<%= party.name %>

However, I get the following error:

undefined method `name' for nil:NilClass

I am at my end, someone please help: - |

In addition, this is the code for the controller, which displays a view containing a partial (controller named default_controller):

def index
    @parties = Party.all
end

Is it really not a fact that this is not a party_controller?

+5
source share
1 answer

I tried something like below and it worked

<%= render :partial => 'party', :object => party  %>

and i can access like party.name. the local variable is called after the partial name that is here party.

: , parties_controller. .

Update:

class PostsController < ApplicationController
    #... ...
    def index
        @posts = Post.all
        @comments = Comment.all #<---- Loading comments from PostsController
        #... ...
    end  
end

#views/posts/index.html.erb

<% @comments.each do |comment| %>
    <%= render :partial=>"comments/comment", :object=>comment %>
<% end %>

#views/comments/_comment.html.erb

<%= comment.body %>

:)

+10

All Articles