Reputation: 155
I'm using ruby on rails form helper.In the form I created I have several input fields: web_update(boolean), value1(int),value2(int),value3(int)
<div style="margin-left:30px">
Load Value From Web Site
<%= f.check_box :load_web %>
</div>
<div class="control-group">
<%= f.label :value1, class: "control-label" %>
<div class="controls">
<%= f.text_field :value1, class: "input-large" %>
</div>
</div>
when I set load_web to true, which means the value1-value3 field and the load_web column will be updated to database.This works fine.
But when I set load_web to false,which I want only update the load_web value in database,this form update value1-value3 fields as well,and update them as 0,while I expect it to keep its orig value.
below is the params I get when set load_web to false and update form:
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"YwbqeRIjadhMlPIA9fjc0Nm66p1bcu786HhaukofykU=",
"project_id"=>"34",
"load_web"=>"0",
"value1"=>0.0,
"value2"=>0.0,
"value3"=>0.0},
"commit"=>"Submit",
"id"=>"9"}
How can I remove value1-value3 from update according to current load_web value? Or are there other ways to solve it?
Upvotes: 0
Views: 1735
Reputation: 118271
Here is one way, you can do. You can have 2 strong parameter declarations.
private
def load_web_set_params
params.permit(:load_web, :value1, :value2, :value3,..)
end
def load_web_unset_params
params.permit(:load_web,...) # don't put value1 to value3 here
end
Now, Inside the controller, before update test the value of load_web
and use the corresponding the method to update.
case load_web_unset_params[:load_web]
when '0' then Model.update load_web_unset_params
when '1' then Model.update load_web_set_params
end
Upvotes: 1