Rails 5-在更新时手动将属性添加到控制器中的参数

Mat*_*lis 0 ruby-on-rails actioncontroller ruby-on-rails-5

我试图在完成表单后手动向控制器中的参数添加其他属性。参数似乎包裹在中,这似乎阻止了我更改参数。

控制器:

class ActualsController < SignedInController
...
 def update
   respond_to do |format|
   facesheet = actual_params[:facesheet]

   #trying to manually add attribute
   actual_params.merge(:file_name => 'ted.png')
   new_params = actual_params.except(:facesheet)

      if @actual.update(new_params)
        format.html { redirect_to action: 'show', id:@actual.id, notice: 'Actual was successfully updated.' }
        format.json { render :show, status: :ok, location: @actual }
      else
        format.html { render :edit }
        format.json { render json: @actual.errors, status: :unprocessable_entity }
      end

   end
 end
...
end
Run Code Online (Sandbox Code Playgroud)

控制台中的“ new_params”

<ActionController::Parameters {"encounter_type_id"=>"1", "physician_id"=>"669", "insurance_id"=>"1182", "time_start"=>"05:00", "time_end"=>"07:00", "date_start"=>"2017-08-02", "date_end"=>"2017-08-02", "facility_id"=>"1", "med_rec_num"=>"1244", "patient_name_first"=>"Bob", "patient_name_last"=>"Smith", "patient_name_middle_initial"=>"H", "patient_dob"=>"2000-02-05", "note"=>"", "is_complete"=>"0", "procedure_ids"=>["", "300"]} permitted: true>  -Thanks for your help on this.
Run Code Online (Sandbox Code Playgroud)

控制台中的“ @actual”

#<Actual id: 18, encounter_id: 4, provider_id: 7, encounter_type_id: 1, physician_id: 669, facility_id: 1, insurance_id: 1182, group_id: nil, datetime_start_utc: "2017-08-02 10:00:00", datetime_end_utc: "2017-08-02 12:00:00", payer: nil, med_rec_num: "1244", patient_name_first: "Bob", patient_name_last: "Smith", patient_name_middle_initial: "H", patient_dob: "2000-02-05", finclass: nil, is_valid: nil, is_complete: false, image_location: nil, note: "", created_at: "2017-08-18 13:30:58", updated_at: "2017-08-18 16:01:28">
Run Code Online (Sandbox Code Playgroud)

Sim*_*ime 5

你正在使用merge哪个

返回一个新的ActionController::Parameters,其中所有键都other_hash合并到当前哈希中

并且您没有将新内容存储ActionController::Parameters到任何内容中。您可以改为使用merge!哪个

返回合并到当前哈希中的当前ActionController::Parameters实例other_hash

actual_params.merge!(:file_name => 'ted.png')
Run Code Online (Sandbox Code Playgroud)

如果actual_params是控制器上的一个方法而不是一个对象(这个名字第一次让我震惊,但看到模型名称可能是Actual,现在方法更有意义),你需要将返回值存储到一个新的哈希中与:

new_params = actual_params.merge(:file_name => 'ted.png')
new_params = new_params.except(:facesheet)
@actual.update(new_params)
Run Code Online (Sandbox Code Playgroud)

这是因为每次调用定义如下的方法时:

def actual_params
  params.require(:actual).permit(:factsheet, *whatever_else)
end
Run Code Online (Sandbox Code Playgroud)

ActionController::Parameters每次都会返回一个新的、不同的,所以如果你不将它保存在任何地方,那么改变它仍然不起作用


max*_*max 5

通常,将传入参数视为不可更改是一种很好的做法。它使调试和推理有关您的控制器代码变得更加简单。

您可以采取一些技巧来避免乱搞参数。

使用块:

# this works for .new and .create as well
updated = @actual.update(actual_params) do |a|
  a.foo = current_user.foo
  a.b = params.fetch(:foo, "some default")
end
Run Code Online (Sandbox Code Playgroud)

使用合成来创建安全的参数方法。

def common_params
   [:name, :title]
end

def create_params
  params.require(:model_name)
        .permit(*common_params, :foo)
end

def update_params
  params.require(:model_name)
        .permit(*common_params, :bar)
end
Run Code Online (Sandbox Code Playgroud)

使用.except可能会有风险,因为使用白名单比黑名单更好,因为您可能会忘记将将来的属性添加到黑名单。