ruby on rails - How to overide user selected attribute based on another attribute's value -


i have form want checkbox selection override user's selected value attribute.

the form:

<%= form_for(@foo) |f| %>   <tr>           <th>footype</th>     <td><%= f.select :foo_type_id, options_from_collection_for_select(footype.order(:name).all, "id", "name", @foo.cabinet_type_id) %></td> </tr> <tr>   <th>legacy foo</th>   <td><%= f.check_box :is_legacy %> </td> </tr> 

where put code overwrite user's selection :foo_type based on if :is_legacy checked? tried adding update_attribute command after saving new foo, doesn't seem work:

if @foo.save_standard , ! row.nil?   #set foo type 38u when legacy foo   if params[:is_legacy] == true     @foo.update_attribute(foo_type_id, 2)  end 

i think there couple of mistakes in following code:

if @foo.save_standard , ! row.nil?   #set foo type 38u when legacy foo   if params[:is_legacy] == true     @foo.update_attribute(foo_type_id, 2)   end end 

try:

if @foo.save_standard && row   if params[:foo][:is_legacy]     @foo.update_attribute(:foo_type_id, 2) # careful, method not fire callbacks   end end 

assuming instance object foo class (which not). if not, replace

params[:foo] 

by whatever instance class is.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -