我一直在遵循rails指南创建和安装一个引擎这里.Created博客文章,当我试图评论时,它返回了"ActiveModel::ForbiddenAttributesError in Blorgh::CommentsController#create“错误。注释控制器
require_dependency "blorgh/application_controller"
module Blorgh
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment])
flash[:notice] = "Comment has been created!"
redirect_to posts_path
end
end
end下面是评论模型
module Blorgh
class Comment < ActiveRecord::Base
end
end如何解决这个问题?
发布于 2013-10-22 05:37:09
我猜您使用的是rails 4。您需要在这里标记所有必需的参数,如下所示:
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(post_params)
flash[:notice] = "Comment has been created!"
redirect_to posts_path
end
def post_params
params.require(:blorgh).permit(:comment)
end希望此链接能帮上忙。
发布于 2014-01-20 01:41:41
我也犯了同样的错误。因此,如果您删除了params,那么很容易看到带有文本键的嵌套注释params。似乎本教程是针对Rails 3的,因此对于rails 4方法,需要进行的更改是添加comment_params方法,如下所示。
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"uOCbFaF4MMAHkaxjZTtinRIOlpMj2QSOYf+Ugn5EMUI=",
"comment"=>{"text"=>"asfsadf"},
"commit"=>"Create Comment",
"post_id"=>"1"}
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
flash[:notice] = "Comment has been created!"
redirect_to posts_path
end
private
# Only allow a trusted parameter "white list" through.
def comment_params
params.require(:comment).permit(:text)
endhttps://stackoverflow.com/questions/19509573
复制相似问题