Есть модели User, Articles, Comments. Использую свзязь has_many :through когда Comments привязан к User и Articles.
Отправляю комментарий, ни каких ошибок не получаю (страница перезагружается), в базе тоже чисто.
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
В логах:
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)
В чем дело?
Добавил модель
class Comment < ApplicationRecord
belongs_to :article
belongs_to :user
end
create
наcreate!
и посмотрите ошибку – Mikhail Vaysman 6 фев '17 в 11:53Comments
в вопрос – Mikhail Vaysman 6 фев '17 в 14:46