5

This is my code for rendering the partial (the @parties collection is being generated correctly, I have tested that):

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

And this is the code in the partial:

<%= party.name %>

However, I get the following error:

undefined method `name' for nil:NilClass

I'm at my wits end, someone please help :-|

Also, this is the code for the controller to render the view containing the partial (The controller's called default_controller):

def index
    @parties = Party.all
end

Is it of any consequence that this isn't the parties_controller?

1

1 Answer 1

11

I've tried something like below and it worked

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

and I can access like party.name. the local variable is named after the partial name which is party here.

Note: Im assuming that your both partials are of parties_controller. So this should work.

Update: Here is what ive tried with again

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 %>

And its working :)

Sign up to request clarification or add additional context in comments.

5 Comments

Though the link apidock.com/rails/ActionController/Base/render is deprecated, it provides some good examples of render.
The page which uses the partial is not rendered throught the parties_controller - why is that a problem? (The code for the controller being used still gets all parties from the database using @parties = Party.all)
Ive updated my post with another example. Here is the tiny rails project that im giving example from. drive.google.com/file/d/0Byto7mT_V2AyOGNaODNKbmU1X2M/…. You can download and can see in action yourself :)
I literally didn't change a single line of code, just restarted my computer and now it works - christ. Thank you very much for taking the time to help anyway :-) (your solution is exactly what I was doing)
oh thats great :) may be it required a rails server restart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.