Michael Peralta
Michael Peralta

Reputation: 293

undefined method `comments' for nil:NilClass

I'm trying to create a way to have users comment on my posts. Currently I have All user posts showing up on my home page and then in the user profile only the current users posts. I would like to have it so that the comments appear only on the posts in the users profile. I tried to add a comment form in the user profile but I got an undefined method `comments' for nil:NilClass error.

My comments_controller looks like

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post])
    @comment = @post.comments.create(params[:comment])
    redirect_to post_path(@post)
end

I have a partial (_comment_form.html.erb) that I am rendering in the user profile which looks like

<h2>Add a comment:</h2>
<%= form_for ([@post, @post.comments.build]) do |f| %>
   <div class="field">
    <%= f.label :commenter %><br />
    <%= f.text_field :commenter %>
   </div>
   <div class="field">
    <%= f.label :body %><br />
    <%= f.text_area :body %>
   </div>
   <div class="actions">
    <%= f.submit %>
   </div>
<% end %>

My Comment model looks like

class Comment < ActiveRecord::Base
  belongs_to :post
end

My Post model looks like

class Post < ActiveRecord::Base
 attr_accessible :content

 belongs_to :user

 validates :content, :presence => true
 validates :user_id, :presence => true
 validates :user, :presence => true
 validates :title, :presence => true

 has_many :comments

 default_scope :order => 'posts.created_at DESC'
end

My User Profile Looks like show.html.erb

<table class="profile" summary="Profile information">
  <tr>
    <td class="main">
    <h1>
        <%= gravatar_for @user %>
        <%= @user.name %>
    </h1>
    <% unless @user.posts.empty? %>
        <table class="posts" summary="User posts">
            <%= render @posts %>
            <%= render 'comments/comment_form' %>
        </table>    
    <% end %>
    </td>
    <td class="sidebar round">
  <strong>Name</strong> <%= @user.name %><br />
  <strong>URL</strong>  <%= link_to user_path(@user), @user %><br />
  <strong>Tasks</strong> <%= @user.posts.count %>
    </td>
  </tr>
</table>

Upvotes: 0

Views: 6686

Answers (5)

Dafeng
Dafeng

Reputation: 118

Can you see log/development.log to see where the error occurred? It wasn't clear from the question. But judging from your code, there are two possible locations:

  1. @comment = @post.comments.create(params[:comment]) here is unlikely because the last line of code is Post.find which will raise a RecordNotFound if the id is not found

  2. <%= form_for ([@post, @post.comments.build]) do |f| %>

This is very likely, can you do a puts @post.inspect and check your development.log to see if that is null. Assuming that it is null, you need to instantiate a Post object wherever you rendered _comment_form.html.erb

Upvotes: 0

Ashwin kumar
Ashwin kumar

Reputation: 21

@post = Post.find_by_id(params[:post_id])

Upvotes: 2

Oscar Barrett
Oscar Barrett

Reputation: 3275

<%= render @posts %>

This line should reference @post instead. Note the trailing s, compared to all the other references to it in your code.

Upvotes: 1

Moiz Raja
Moiz Raja

Reputation: 5762

Are you initializing the @post in the show action of your PostsController? That would be required because you are redirecting from the create action of your CommentsController.

Upvotes: 1

tadman
tadman

Reputation: 211740

It might be that you haven't initialized @post in the new method of your controller and it's being used as nil. Always construct an empty model for your new form if it's practical:

def new
  @post = Post.new(params[:post])
end

Upvotes: 2

Related Questions