我一直在遵循创建和安装引擎的 Rails 指南 here创建博客文章,当我尝试发表评论时,它返回“ActiveModel::ForbiddenAttributesError in Blorgh::CommentsController#create”错误。 评论 Controller
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
如何解决这个问题?
最佳答案
我猜你使用的是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
希望 this link 帮助...
关于ruby-on-rails - ActiveModel::ForbiddenAttributesError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19509573/