【问题标题】:ActiveModel::ForbiddenAttributes with Rails engine带有 Rails 引擎的 ActiveModel::ForbiddenAttributes
【发布时间】:2013-05-27 13:16:22
【问题描述】:

我正在使用 Rails 引擎(社交流),它在 rails 3 应用程序中使用 strong_parameters gem。尝试更新时出现错误,但无法确定问题出在哪里。

我在更新操作中使用模式 ajax 表单,带有 update_attributes。我已将要更新的属性添加到私有 allowed_pa​​rams 方法,但我得到了一个 ActiveModel::ForbiddenAttributes (ActiveModel::ForbiddenAttributes):错误

ActiveModel::ForbiddenAttributes (ActiveModel::ForbiddenAttributes):
  <a href="txmt://open?  url=file:///Users/sean/Dropbox/fluent/fluent100/app/controllers/sentences_controller.rb&amp;line=18&amp;column=1">app/controllers/sentences_controller.rb:18:in `update'</a>

使用有关强参数的文档 (https://github.com/rails/strong_parameters#readme),我试图确定问题属性可能是什么,但 rails 日志没有提供任何信息。

我尝试在 development.rb 中设置 config.action_controller.action_on_unpermitted_pa​​rameters = :log 但我的应用程序将不会以:

undefined method `action_on_unpermitted_parameters=' for ActionController::Base:Class (NoMethodError)

来自我的控制器:

def update
  @sentence.update_attributes params[:sentence]
    if @sentence.valid?
    flash[:notice] = 'Sentence was successfully updated.'
  end
  .
  .

 private
   def allowed_params
     [:id, :title, :text, :description, :difficulty, :sentence]
   end

在 gem 代码中引用了 allowed_pa​​rams 方法,并且似乎将允许的参数传递给 strong_paramaters gem。

protected
    def whitelisted_params
      return {} if request.present? and request.get?

      params.require(self.class.model_class.to_s.underscore.to_sym).permit( *all_allowed_params )
    end

    def allowed_params
    [] # This should be overriden in controllers to allow extra params
  end

  def all_allowed_params
    COMMON_PARAMS  |
      activity_object_property_params |
      allowed_params
  end

补充说明:

https://groups.google.com/forum/?fromgroups#!searchin/social-stream/whitelist/social-stream/_aManxsvGHI/_XNHZVwB1C4J

* 更新 * 更新似乎在我使用时起作用:

update!

而不是

@sentence.update_attributes params[:sentence]

我不清楚为什么。

【问题讨论】:

    标签: ruby-on-rails strong-parameters social-stream


    【解决方案1】:

    update! 方法有效,我选择使用它。

    def update
       update! do |success, failure|
         failure.html { render 'edit_modal', layout: false }
         success.html {  
            load_sentences
            render partial: 'table', locals: { sentences: @sentences }
         }
       end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 2013-02-08
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      相关资源
      最近更新 更多