Ruby on Rails Thursday, August 22, 2013

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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.

No comments:

Post a Comment