1

I am trying to follow the RailsGuides, http://edgeguides.rubyonrails.org/getting_started.html

Now, in section 6.4 where I am supposed to be able to add comments in my blog site, I am encountering this error:

NoMethodError in Posts#show
Showing C:/Sites/blog/app/views/posts/show.html.erb where line #12 raised: </br>
undefined method `comments' for #<Post:0x3a99668

below is the snippet in my show.html.erb

<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
 <p>
   <%= f.label :commenter %><br>
   <%= f.text_field :commenter %>

Below is the snippet for my comments controller:

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

  private
    def comment_params
      params.require(:comment).permit(:commenter, :body)
    end
end

Below is my Posts controller:

class PostsController < ApplicationController
def new
     @post = Post.new
end

def create
    @post = Post.new(params[:post].permit(:title, :text))

    if @post.save
        redirect_to @post
    else
        render 'new'
    end
end

def show
    @post = Post.find(params[:id])
end

def index
    @posts = Post.all
end

def edit
    @post = Post.find(params[:id])
end

def update
    @post = Post.find(params[:id])

    if @post.update(params[:post].permit(:title, :text))
        redirect_to @post
    else
        render 'edit'
    end
 end

 def destroy
      @post = Post.find(params[:id])
      @post.destroy

      redirect_to posts_path
     end

private
  def post_params
    params.require(:post).permit(:title, :text)
  end
end

Please help me thanks.

-aguds

aguds
  • 21
  • 5
  • Make sure you have correctly followed section 6.2 of the same guide. – Bot Aug 18 '13 at 19:28
  • Yes I did.. In fact I already tried copy pasting the code snippets from the site itself. – aguds Aug 18 '13 at 20:18
  • Can i see your app/modelss/post.rb file? And meanwhile did you try restarting your server in case you are running in production mode. – Bot Aug 18 '13 at 20:22
  • I tried to restart my server and the code is now working.. I do not what happened. Thanks for the help – aguds Aug 18 '13 at 20:31
  • I do not gave enough reputation points to vote for the answer. But will do – aguds Aug 18 '13 at 21:16
  • You can still accept by checking the right mark. – Bot Aug 18 '13 at 21:18

1 Answers1

0

It's saying

"line #12 raised: undefined method `comments' for #< Post:0x3a99668 >"

That means in line 12 of your show.html.erb you are calling comments method on an object of Post, but that method is not defined.

So you need to check in your Post class that following relation is there.

app/models/post.rb

has_many :comments

The server needs to restarted once you add a new Model. (You added Comment.)

The general rule of thumb here is making changes to anything outside of app/ or config/routes.rb will require a restart.

Refer: When do I need to restart server in Rails?

Community
  • 1
  • 1
Bot
  • 1,001
  • 2
  • 13
  • 32
  • I have added the has_many in post.rb already. But still I encounter the same error. I will edit my question to include other code snippets – aguds Aug 18 '13 at 20:11