Gibson
Gibson

Reputation: 2085

Undefined method each with nested resources

I'm trying to follow Ryan Bates Polymorphic association tutorial in order to put some comments to my site.

The thing is I have nested resources:

  #Nesting Resources
  resources :users do
    resources :photos do
      resources :comments
      resources :tags
    end
  end

So I'm getting an error in my view (photos/show)

undefined method `each' for nil:NilClass

I suppose the problem is in my controller, with comments, that is not defined correctly, but since I have nested resources, I don't know how to do it.

Photos Controller

def show
    @photo = Photo.friendly.find(params[:id])
    @user = @photo.user
    @commentable = @photo
    @comments = @commentable.comments
    @comment = Comment.new
end

New Comment Partial

  <h2>Comments</h2>

<% if @comments.any? %>
    <%= render "comments/comments" %>
<% else %>
    Todavía no hay comentarios
<% if user_signed_in? %>
    <%= render "comments/form" %>
<% end %>
<% end %>

Form partial (comments/form)

<%= form_for [@commentable, @comment] do |f| %>
  <% if @comment.errors.any? %>
    <div class="error_messages">
      <h2>Please correct the following errors.</h2>
      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.text_area :content, rows: 8 %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Comments partial (comments/comments)

<div id="comments">
  <% @comments.each do |comment| %>
    <div class="comment">
      <%= simple_format comment.content %>
    </div>
  <% end %>
</div>

Upvotes: 3

Views: 515

Answers (1)

Mandeep
Mandeep

Reputation: 9173

Your error says undefined method each for nil:NilClass. As you can guess you are calling each on a nil object. Here your @comments is nil and hence giving your trouble.

In your view try something like this:

<div id="comments">
  <% if @comments %>
    <% @comments.each do |comment| %>
      <div class="comment">
        <%= simple_format comment.content %>
      </div>
    <% end %>
  <% end %>
</div>

Edit:

So if you look at your code you have @comments till here:

<% if @comments.any? %>
  <%= render "comments/comments" %>
<% else %>
  #do stuff
<% end %>

it's after you call your partial that your @comment is lost so try this:

<% if @comments.any? %>
  <%= render "comments/comments", comments: @comment %>
<% else %>
  #do stuff
<% end %>

and then in your view use

<div id="comments">
  <% comments.each do |comment| %>
    <div class="comment">
      <%= simple_format comment.content %>
    </div>
  <% end %>
</div>

Upvotes: 1

Related Questions