ActiveModel :: ForbiddenAttributesError – Rails 4
我有点迷茫。 我得到了Rails 4错误:ActiveModel :: ForbiddenAttributesError。 我明白这意味着我需要允许物品通过,我已经完成了,但我必须遗漏一些东西。
评论控制器:
class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create!(params[:comment]) redirect_to @post end private # Never trust parameters from the scary internet, only allow the white list through. def comment_params params.require(:comment).permit(:post_id, :comment, :body) end end
创建评论迁移
class CreateComments < ActiveRecord::Migration def change create_table :comments do |t| t.references :post t.text :body t.timestamps end end def self.down drop_table :comments end end
我在这里想念的是什么? 如果您需要查看任何其他代码,请告诉我们。
谢谢!
代替
@comment = @post.comments.create!(params[:comment])
你要
@comment = @post.comments.create!(comment_params)
你没有使用允许的属性就完成了所有艰苦的工作!