There are models User, Articles, Comments. I use the has_many: through link when Comments is tied to User and Articles.

I am sending a comment, I do not receive any errors (the page reloads), the database is also clean.

class CommentsController < ApplicationController def create @article = Article.find(params[:article_id]) logger.debug "@ARTICLE VAL: #{@article.attributes.inspect}" @comment = @article.comments.create(comment_params) logger.debug "@COMMENT PARAMS: #{comment_params.inspect}" redirect_to article_path(@article) end private def comment_params params.require(:comment).permit(:body) end end 

In the logs:

 Started POST "/articles/6/comments" for 127.0.0.1 at 2017-02-06 13:14:56 +0200 Processing by CommentsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"***", "comment"=>{"body"=>"11111"}, "commit"=>"Create Comment", "article_id"=>"6"} [1m[36mArticle Load (3.2ms)[0m [1m[34mSELECT "articles".* FROM "articles" WHERE "articles"."id" = ? LIMIT ?[0m [["id", 6], ["LIMIT", 1]] @ARTICLE VAL: {"id"=>6, "title"=>"Article 4", "text"=>"qwer", "created_at"=>Mon, 06 Feb 2017 05:25:11 UTC +00:00, "updated_at"=>Mon, 06 Feb 2017 05:25:11 UTC +00:00, "user_id"=>1} [1m[35m (0.1ms)[0m [1m[36mbegin transaction[0m [1m[35m (0.1ms)[0m [1m[36mcommit transaction[0m @COMMENT PARAMS: <ActionController::Parameters {"body"=>"11111"} permitted: true> Redirected to http://localhost:3000/articles/6 Completed 302 Found in 14ms (ActiveRecord: 3.4ms) 

What's the matter?


Added a model

 class Comment < ApplicationRecord belongs_to :article belongs_to :user end 
  • one
    try replacing create with create! and see the error - Mikhail Vaysman
  • I get Validation failed: User must exist @comment = @ article.comments.create! (Comment_params) - Sergei R
  • one
    here is the answer. - Mikhail Vaysman
  • I honestly do not understand him, I have one user in his ID log is "user_id" => 1, which means it does not exist, if it is? - Sergei R
  • add a Comments model to the question - Mikhail Vaysman

1 answer 1

You have validation crashed there. Like, the comment should belong to the user. The requirement is quite meaningful. Hence the question:

Who is posting a comment?

You have no users in the controller mentioned.

Track what fields you have filled in the new comment.

  • In comment_params you only take body .
  • From the @article.comments you will receive the article's foreign key.
    • article_id , most likely.

Where will user_id get to the user_id ?

We need to add it there somehow.


You can create a comment not in one association, but in their mixture with the help of merge , like this:

 @comment = @article.comments .merge(current_user.comments) # <---- добавилось .create(comment_params) 

This, of course, provided that current_user returns the currently logged in user, and it has the correct has_many :comments .


You can screw this parameter into comment_params . It is easier to understand, but it builds an extra assumption about the name of the column with the foreign key of the comment author.

 params.require(:comment) .permit(:body) .merge(user_id: current_user.id) # <---- 

Again, I expect that current_user.id in your application makes sense (which is most likely true, and if not, then you most likely know about it).


user_id other method may be used that will lead to the task in the user_id field user_id .

  • Thank you, option 2 I liked more, now everything works. I thought once in @article glitters "user_id" => 1 then it somehow gets into the comment. - Sergei R
  • @SergeiR and it would turn out that comments on the article are written by its author and only to them, is it really amazing? : D - D-side