Forum: Ruby on Rails I am getting a ' undefined method error ' when saving a comment

563dfbbd7385103f723ed478b98ba828?d=identicon&s=25 ank k. (ank_k)
on 2013-08-22 21:56
hi!! I am a newbie in ROR.

I have two models

Post
has_many :Comment


Comment
belongs_to :Post


the error displays when I try to save a comment related to certain post.
I get the following error
-----------------------------------------------------------------
undefined method `comment' for nil:NilClass
-----------------------------------------------------------------
my comment controllers

  def new
 @comment = Comment.new
render :layout => false
  end

  def create
    @post = Post.find_by_id(params[:id])
    @post_comment = @post.comment.create(params[:comment])
    if @post_comment.save
      redirect_to post_index_path
      flash[:success] = "comment created!!"
    end

Could someone please explain why am I getting this error..

Thanks in advance :)
7c43819aa888fd590262af439e6eae48?d=identicon&s=25 Germano Teixeira (Guest)
on 2013-08-22 22:07
(Received via mailing list)
You shold use

*has_many :comments*

instead of

*has_many :comment*

Then you can call

@post.comments

Look this example:
http://guides.rubyonrails.org/association_basics.h...

Take a look at accepts_nested_attributes to create your posts:
http://api.rubyonrails.org/classes/ActiveRecord/Ne...



2013/8/22 ank k. <lists@ruby-forum.com>
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-22 22:09
(Received via mailing list)
On 22 August 2013 20:56, ank k. <lists@ruby-forum.com> wrote:
> hi!! I am a newbie in ROR.
>
> I have two models
>
> Post
> has_many :Comment

that should be
has_many :comments
note that it is plural and lower case
>
>
> Comment
> belongs_to :Post

belongs_to :post

>  @comment = Comment.new
> render :layout => false
>   end
>
>   def create
>     @post = Post.find_by_id(params[:id])
>     @post_comment = @post.comment.create(params[:comment])

You have not told us which line the error points to but I guess it is
the one above.  If so then it is saying that @post is nil, so your
find_by_id did not find anything.  You don't need to use find_by_id,
you can just use
@post = Post.find( params[:id] )
Perhaps there is not a Post object with the correct id.  If you look
in log/development.log you will see the params you are passing which
may give you a clue.

I suggest, however, that as a beginner you work right through a good
tutorial such as railstutorial.org (which is free to use online).
That will show you the basics of rails.

Also see the Rails Guides.

Colin
Eb46661cc18996545d3a726d04fb772f?d=identicon&s=25 John Wick (9marveric)
on 2017-03-21 10:23
Thank you support.
B6966d88ac4a246ea0379a35bb0e2dec?d=identicon&s=25 Max Pittman (maxpittman)
on 2017-04-18 13:55
THanks
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.