Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何基于另一个属性覆盖用户选择的属性';s值_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何基于另一个属性覆盖用户选择的属性';s值

Ruby on rails 如何基于另一个属性覆盖用户选择的属性';s值,ruby-on-rails,Ruby On Rails,我有一个表单,希望选中复选框以覆盖用户为另一个属性选择的值 表格: <%= form_for(@foo) do |f| %> <tr> <th>FooType</th> <td><%= f.select :foo_type_id, options_from_collection_for_select(FooType.order(:name).all, "id", "name", @foo.cabin

我有一个表单,希望选中复选框以覆盖用户为另一个属性选择的值

表格:

<%= form_for(@foo) do |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>

我认为以下代码中有几个错误:

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

假设您的实例对象是
Foo
类(可能不是)。如果没有,请更换

params[:foo]

无论您的实例类是什么。

使用
(如果我们选中此框,请为is_legacy参数添加真实值),如果参数[:is_legacy],则可能使用
。to_s==“true”
似乎仍然无法实现与我现在使用的逻辑相同的逻辑。您是对的,它们是错误的,不幸的是,它保持了用户数据不变。谢谢。2个问题:为什么有时需要将对象类型放入params[]调用中,方法不触发回调意味着什么?当使用(@foo)的
form_时,您告诉rails将表单值封装在
@foo
类名中。关于回调的更多信息,我强烈建议你看看这个
if @foo.save_standard && row
  if params[:foo][:is_legacy]
    @foo.update_attribute(:foo_type_id, 2) # careful, this method do not fire callbacks
  end
end
params[:foo]