【问题标题】:Rails 4 strong parameters ActiveModel::ForbiddenAttributesErrorRails 4 强参数 ActiveModel::ForbiddenAttributesError
【发布时间】:2013-10-18 18:38:19
【问题描述】:

由于某种原因,在我当前的控制器中,我得到了 ActiveModel::ForbiddenAttributesError,即使我相信我使用强参数就好了。尽管我正在使用许可证!暂时允许所有模型属性。请参阅下面的代码,我错过了什么

class HeuristicsController < ApplicationController

    def index
        @heuristics         = Heuristic.order(:name).page params[:page]
        @heuristic      = Heuristic.new
    end

    def create
        @heuristic = Heuristic.new(params[:heuristic])
        if @heuristic.save
            redirect_to action: 'index', :flash => {:success => "New heuristic created!" }
        else
            render 'new'
        end
    end

    def new
        @title              = "Heuristic"
        @heuristic          = Heuristic.new
    end

    private

    def heuristic_params
        params.require(:heuristic).permit!      
    end

end

【问题讨论】:

    标签: activerecord ruby-on-rails-4 mass-assignment strong-parameters


    【解决方案1】:

    我认为你没有完全理解强参数的工作方式......

    你有方法

    def heuristic_params
        params.require(:heuristic).permit!      
    end
    

    你没有使用它

    Heuristic.new(params[:heuristic])
    

    【讨论】:

    • 我肯定是误会了!我认为通过一些 Rails 魔术,任何对 params 的引用都会以某种方式引用 MODEL_NAME_params
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2013-12-07
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多