Add a comment on user models and publications (Ruby on Rails)

I am new to Rails. I am building my first application - a simple blog. I have User and Post models where each user can write many posts. Now I want to add a comment model, where each message can have many comments, and also each user can comment on any message created by any other user. In the comment model, I have

  id \ body \ user_id \ post_id 

the columns.
Model Associations:
user.rb

has_many :posts, dependent: :destroy has_many :comments 

post.rb

 has_many :comments, dependent: :destroy belongs_to :user 

comment.rb

 belongs_to :user belongs_to :post 

So, how to define the create action in CommentsController correctly? Thanks.

UPDATE:
routes.rb

 resources :posts do resources :comments end 

comments_controller.rb

  def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params) if @comment.save redirect_to @post else flash.now[:danger] = "error" end end 

Result

 --- !ruby/hash:ActionController::Parameters utf8: βœ“ authenticity_token: rDjSn1FW3lSBlx9o/pf4yoxlg3s74SziayHdi3WAwMs= comment: !ruby/hash:ActionController::Parameters body: test action: create controller: comments post_id: '57' 

As we can see, it does not send user_id and only works if I delete validates :user_id, presence: true line from comment.rb

Any suggestions?

+6
source share
3 answers

Along the way, you should put this:

 def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(comment_params) @comment.user_id = current_user.id #or whatever is you session name if @comment.save redirect_to @post else flash.now[:danger] = "error" end end 

And also you should remove user_id from comment_params as strong parameters. Hope this helps you.

+11
source

Associations

To give you a definition of what is going on here, you must remember that when you create a record, you basically populate the database. Your associations are defined using foreign_keys

When you ask how to "add comments to User and Post model" - you do not do this in the bottom line; you add a comment to the Comment model and can associate it with User and Post :

 #app/models/comment.rb Class Comment < ActiveRecord::Base belongs_to :user belongs_to :post end 

This calls Rails to search for user_id and post_id in the default Comment model.

This means that if you want to create a comment directly, you can associate it with any of these associations by simply filling out foreign_keys as you wish (or using Rails objects to populate them).

So, when you want to keep Comment , you can do this:

 #app/controllers/comments_controller.rb Class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) end private def comment_params params.require(:comment).permit(:user_id, :post_id, :etc) end end 

Conversely, you can handle it using standard Rails objects (as per accepted answer)

+7
source
 Class CommentsController < ApplicationController before_action :set_user before_action :set_post def create @comment = @post.comments.create(comment_params) if @comment.save redirect_to @post else flash.now[:danger] = "error" end end private set_post @post = User.posts.find(params[:post_id]) end set_user @user = User.find(params[:user_id]) end comment_params params[:comment].permit() end 
0
source

Source: https://habr.com/ru/post/971469/


All Articles