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
createwithcreate!and see the error - Mikhail VaysmanCommentsmodel to the question - Mikhail Vaysman