0

我正在使用 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

allowed_pa​​rams 方法在 gem 代码中被引用,并且似乎将允许的参数传递给 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]

我不清楚为什么。

4

1 回答 1

0

更新方法有效,我选择这样做。

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
于 2013-05-27T14:16:56.433 回答