Rails 5 - Manually add attribute to parameters in controller when updating -


i trying manually add additional attribute parameters in controller after completing form. params seem wrapped in , seems preventing me changing params.

controller:

class actualscontroller < signedincontroller ...  def update    respond_to |format|    facesheet = actual_params[:facesheet]     #trying 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 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 

"new_params" console

<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 on this. 

"@actual" console

#<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"> 

in general practice view incoming parameters unmutable. makes debugging , reasoning controller code simpler.

there few tricks can avoid monkeying params.

use block:

# works .new , .create updated = @actual.update(actual_params) |a|   a.foo = current_user.foo   a.b = params.fetch(:foo, "some default") end 

use composition create safe parameters methods.

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 

using .except can risky since better whitelist blacklist may forget add future attributes blacklist.


Comments

Popular posts from this blog

The SDK directory does not exist (ubuntu 16.04) (react native) -

python - How to clone or copy a list? -

string - Using python and postgres, variables within execute function? -