ank k.
2013-Aug-22 19:56 UTC
I am getting a '' undefined method error '' when saving a comment
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 :) -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7d1c4682a1e885d80a8023e854db5230%40ruby-forum.com. For more options, visit https://groups.google.com/groups/opt_out.
Germano Teixeira
2013-Aug-22 20:05 UTC
Re: I am getting a '' undefined method error '' when saving a comment
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.html#the-has-many-through-association Take a look at accepts_nested_attributes to create your posts: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html 2013/8/22 ank k. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>> 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 :) > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/rubyonrails-talk/7d1c4682a1e885d80a8023e854db5230%40ruby-forum.com > . > For more options, visit https://groups.google.com/groups/opt_out. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAKj7%3DuRW5HsLrFxPkz6bey3bXg2qtcn%2B%3D44KVh2arTd5rojsMQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Aug-22 20:08 UTC
Re: I am getting a '' undefined method error '' when saving a comment
On 22 August 2013 20:56, ank k. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> hi!! I am a newbie in ROR. > > I have two models > > Post > has_many :Commentthat should be has_many :comments note that it is plural and lower case> > > Comment > belongs_to :Postbelongs_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])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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLvikJNJQYPC8OBMbmrwvtWA3Tgb35ZAgd%3Do%2B2RzQ7XDLw%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.