【问题标题】:Strong Parameters claims param not found when it's present强参数声明参数存在时未找到
【发布时间】:2013-11-24 04:16:12
【问题描述】:

所以由于某种原因,我收到了ActionController::ParameterMissing 错误。它说incident 参数丢失,但它明显存在:

Started PATCH "/incidents/16/assign-score" for 127.0.0.1 at 2013-11-23 23:07:12 -0500
Processing by IncidentsController#update_override_score as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"RSGaHrrTrO5DoX8dVEtVNQX8OJnRAD35YRSCAvZtNr4=", "incident"=>{"id"=>"16", "score_override"=>"5"}, "commit"=>"Save legitimacy score", "id"=>"16"}
Unpermitted parameters: id, score_override
  Incident Load (0.1ms)  SELECT "incidents".* FROM "incidents" WHERE "incidents"."id" = ? LIMIT 1  [["id", "16"]]
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
  CACHE (0.0ms)  SELECT "incidents".* FROM "incidents" WHERE "incidents"."id" = ? LIMIT 1  [["id", "16"]]
Completed 400 Bad Request in 9ms

ActionController::ParameterMissing (param not found: incident):
  app/controllers/incidents_controller.rb:66:in `update_override_score'


  Rendered /Users/justinbull/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.6ms)
  Rendered /Users/justinbull/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /Users/justinbull/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /Users/justinbull/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (62.8ms)

相关控制器代码:

class IncidentsController < ApplicationController
  load_and_authorize_resource
  before_action :set_incident, only: [:show, :edit, :update, :destroy, :edit_override_score, :update_override_score]

  # ...

  def update_override_score
    override_params = params.require(:incident).permit(:score_override)
    override_params[:score_override] = nil if override_params[:score_override].blank?

    respond_to do |format|
      if @incident.update_attributes! score_override: override_params[:score_override]
        format.html { redirect_to @incident, notice: 'Incident was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit_override_score' }
        format.json { render json: @incident.errors, status: :unprocessable_entity }
      end
    end
  end

  # ...

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_incident
      @incident = Incident.find(params[:id])
    end
end

知道这怎么可能发生吗?您可以在日志的“参数”行中看到事件哈希。

【问题讨论】:

  • 看起来您正在使用 CanCan,是吗? CanCan 和强参数存在已知问题——例如,参见this oneHere's 可能与您的问题相关的对我有用的解决方案。
  • @JoshLeitzel 很好看。是康康。感谢您的提示!

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


【解决方案1】:

感谢 Josh Leitzel,他为我指明了正确的方向。我正在使用 CanCan gem,它在 1.6.10 版本中遇到 Rails 4 问题。他的评论解释了解决方法。

但是,我不得不将 param.require() 逻辑移动到控制器方法 incident_params 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2021-08-23
    • 2017-02-16
    • 1970-01-01
    • 2017-05-31
    • 2012-01-26
    • 2017-03-25
    相关资源
    最近更新 更多