强参数声称当它存在时找不到参数

时间:2013-11-24 04:16:12

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

因此出于某种原因我收到了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

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

1 个答案:

答案 0 :(得分:1)

感谢Josh Leitzel,他指出了我正确的方向。我正在使用CanCan宝石,它是1.6.10,与Rails 4有问题。他的评论解释了解决方法。

然而,我必须将param.require()逻辑移动到控制器方法incident_params中。