3

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>
Gibson
  • 2,055
  • 2
  • 23
  • 48

1 Answers1

1

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>
Mandeep
  • 9,093
  • 2
  • 26
  • 36
  • thanks for responding. It wont work...It keeps saying <%= form_for [@commentable, @comment] do |f| %>... Do I need to pass into the form params user_id somewhere? Since I'm dealing with nested resources – Gibson Jun 29 '14 at 15:20
  • @Gibson yeah you need to pass user id in a hidden field or use it in controller method and by the way that's a problem in form and not your view :) – Mandeep Jun 29 '14 at 15:25
  • but the problem is the same... I'm still getting nil on objects and don't know why... – Gibson Jun 29 '14 at 16:41
  • can you paste your full view file? – Mandeep Jun 29 '14 at 16:44
  • It's not working... Comments are being lost in new_comment partial so that means Comments are nil in Photo Controller, thats where the problem comes from...But I don't know how to deal with it! – Gibson Jun 29 '14 at 17:25