我正在尝试在完成表单后手动向控制器中的参数添加其他属性。这些参数似乎被包裹起来,似乎阻止我改变参数。
控制器:
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
&#34; new_params&#34;从控制台
<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.
&#34; @实际&#34;从控制台
#<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">
答案 0 :(得分:3)
通常,将传入参数视为不可变的是一种很好的做法。它使您对控制器代码的调试和推理变得更加简单。
你可以采取一些措施来避免使用params。
使用块:
void myGraphicsView::mouseReleaseEvent(QMouseEvent *event) {
qDebug()<<" Mouse relase pressed at x="<<event->x()<<" y ="<<event->y();
endPos = new QPoint;
endPos->setX(event->x());
endPos->setY(event->y());
c->addLine(0,0,event->x(),50);
}
使用合成来创建安全参数方法。
# 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
使用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
可能会有风险,因为白名单比黑名单更好,因为您可能忘记将未来的属性添加到黑名单。
答案 1 :(得分:2)
尝试类似
private
def post_params
params.require(:post).permit(:some_attribute).merge(user_id: current_user.id)
end
答案 2 :(得分:1)
您正在使用merge
返回一个新的
中ActionController::Parameters
,其中other_hash
的所有键都合并到当前哈希
并且您没有将新ActionController::Parameters
存储到任何内容中。您可以改为使用merge!
返回当前
ActionController::Parameters
实例,其中other_hash
合并为当前哈希值。
actual_params.merge!(:file_name => 'ted.png')
如果actual_params
是你控制器上的一个方法而不是一个对象(这个名字第一次告诉了我,但看到模型名称可能是Actual
,一个方法现在更有意义了),你和#39; ll需要将返回值存储到新哈希中以使用:
new_params = actual_params.merge(:file_name => 'ted.png')
new_params = new_params.except(:facesheet)
@actual.update(new_params)
这是因为每次调用的方法都定义为:
def actual_params
params.require(:actual).permit(:factsheet, *whatever_else)
end
它每次都会返回一个新的,不同的ActionController::Parameters
,所以如果你不将它保存在任何地方,那么它仍然无法正常工作
答案 3 :(得分:1)
尝试将params列入白名单,而不是使用.except
还尝试使用.permit()将所需的参数列入白名单