Answer the question
In order to leave comments, you need to log in
How to connect 3 Ruby on Rails models?
user.rb: has_many :comments
post.rb: has_many :comments
comment.rb:
belongs_to :post
belongs_to :user
@comment = @post.comments.create(comment_params)
Answer the question
In order to leave comments, you need to log in
create
creates an object from the passed parameters and saves it immediately, so adding later
and not saving will not happen.
Instead create
, use build
and save by
hand
@comment = @post.comments.build(comment_params)
@comment.user = current_user
@comment.save
>And by the way, is it possible to write belongs_to: :post, :user
No
In your case, use has_many, through ( documentation )
@comment = @post.comments.create(comment_params)
@comment.user = current_user
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question