2013-11-24 35 views
1

因此,出於某種原因,我收到了ActionController::ParameterMissing錯誤。它說incident參數丟失,但它顯然目前Strong Parameters聲明參數在找到時未找到

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 

任何想法,這怎麼會發生呢?您可以在日誌的參數行中看到正確的事件散列。

+2

看起來你使用的是CanCan,是嗎? CanCan和強參數存在已知問題 - 請參閱,例如[this one](https://github.com/ryanb/cancan/issues/835)。 [這裏是](https://github.com/ryanb/cancan/issues/835#issuecomment-18663815)爲我工作的解決方案,可能與您的問題有關。 –

+0

@JoshLeitzel很好看。這是CanCan。謝謝你的提示! – JustinBull

回答

1

感謝Josh Leitzel,他指出了我的正確方向。我使用的CanCan gem是1.6.10,在Rails 4中遇到了麻煩。他的評論解釋瞭解決方法。

但是我必須將param.require()邏輯轉換爲控制器方法incident_params

相關問題