Ruby on rails 4 嵌套表单,在一个表单中更新和创建
我想建立一个表单,在这里我可以编辑一些嵌套对象,同时创建新对象 这就是我现在得到的: 模型: 视图: 现在,如果我单击submit按钮,rails将获取每个填充的行,并使用org_unit_params属性创建一个新的速率。因此,旧的费率不会在我的数据库中多次更新 我想要的是,如果旧记录发生了变化,他会对其进行更新,并为其他记录创建新记录 它应该与创建或更新有关,但我不能把它放在一起 谢谢你的每一个暗示Ruby on rails 4 嵌套表单,在一个表单中更新和创建,ruby-on-rails-4,nested-forms,Ruby On Rails 4,Nested Forms,我想建立一个表单,在这里我可以编辑一些嵌套对象,同时创建新对象 这就是我现在得到的: 模型: 视图: 现在,如果我单击submit按钮,rails将获取每个填充的行,并使用org_unit_params属性创建一个新的速率。因此,旧的费率不会在我的数据库中多次更新 我想要的是,如果旧记录发生了变化,他会对其进行更新,并为其他记录创建新记录 它应该与创建或更新有关,但我不能把它放在一起 谢谢你的每一个暗示 提前感谢并致以最诚挚的问候。好的,我必须在我允许的属性中添加:id: def org_uni
提前感谢并致以最诚挚的问候。好的,我必须在我允许的属性中添加:id:
def org_unit_params
params.require(:org_unit).permit(:id, :name,
rates_attributes: [:id, :name, :tablename, :rate_order_no, :ratevalue, :org_unit_id, :_destroy]
)
end
但这还不够:因此,我不得不在我的字段中添加一个隐藏的输入字段:id,现在它可以工作了:
<%= f.simple_fields_for :rates, :wrapper => false do |ff| %>
<tr class="fields">
<td><%= ff.input :name, label: false %></td>
<td><%= ff.input :tablename, collection: ["Costs","Savings","CnQ"], label:false, prompt: "Select the table" %></td>
<td><%= ff.input :rate_order_no, collection: 1..19, label:false, prompt: "Select the row"%></td>
<td><%= ff.input :ratevalue, label: false, required: true %></td>
<td><%= ff.link_to_remove "Remove", :class => "btn btn-default" %></td>
<%= ff.input :id, :as => :hidden %>
</tr>
<% end %>
好的,将“:id”添加到“允许的属性”中,但它仍然不起作用。任何人
#Organisation Units
get '/org_units/', :to => 'org_units#index', :as => 'org_units'
get '/org_units/:id', :to => 'org_units#show', :as => 'org_unit'
get '/org_units/:id/edit', :to => 'org_units#edit', :as => 'edit_org_unit'
put '/org_units/:id', :to => 'org_units#update'
patch '/org_units/:id', :to => 'org_units#update'
<%= simple_nested_form_for @org_unit do |f| %>
<table id="ratetable" class="display">
<thead>
<tr> <th>Rate Name</th> <th>Table</th> <th>Department</th> <th>Value</th> </tr>
</thead>
<tbody>
<%= f.simple_fields_for :rates, :wrapper => false do |ff| %>
<tr class="fields">
<td><%= ff.input :name, label: false, required: true %></td>
<td><%= ff.input :tablename, collection: ["Costs","Savings","CnQ"], label:false, required: true, prompt: "Select the table" %></td>
<td><%= ff.input :rate_order_no, collection: 1..19, label:false, required: true, prompt: "Select the row"%></td>
<td><%= ff.input :ratevalue, label: false, required: true %></td>
</tr>
<% end %>
</tbody>
</table>
<p><%= f.link_to_add "Add a rate", :rates, :data => { :target => "#ratetable" }, :class => "btn btn-default" %></p>
<br>
<br>
<%= f.submit "Save Rates", :class => "btn bnt-default" %>
<% end %>
def org_unit_params
params.require(:org_unit).permit(:id, :name,
rates_attributes: [:id, :name, :tablename, :rate_order_no, :ratevalue, :org_unit_id, :_destroy]
)
end
<%= f.simple_fields_for :rates, :wrapper => false do |ff| %>
<tr class="fields">
<td><%= ff.input :name, label: false %></td>
<td><%= ff.input :tablename, collection: ["Costs","Savings","CnQ"], label:false, prompt: "Select the table" %></td>
<td><%= ff.input :rate_order_no, collection: 1..19, label:false, prompt: "Select the row"%></td>
<td><%= ff.input :ratevalue, label: false, required: true %></td>
<td><%= ff.link_to_remove "Remove", :class => "btn btn-default" %></td>
<%= ff.input :id, :as => :hidden %>
</tr>
<% end %>