【问题标题】:ActiveModel::ForbiddenAttributesError - Rails 4ActiveModel::ForbiddenAttributesError - Rails 4
【发布时间】:2013-09-08 02:15:37
【问题描述】:

我有点迷茫。我收到 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

我在这里缺少什么?如果您需要查看任何其他代码,请告诉我。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    代替

    @comment = @post.comments.create!(params[:comment])
    

    你想要的

    @comment = @post.comments.create!(comment_params)
    

    您在没有使用允许的属性的情况下完成了所有艰苦的工作!

    【讨论】:

    • 废话!我知道这一定是微不足道的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多