我有一个模型,即“bank_accounts”,其中包含许多用户
我想使用表单列出所有用户(而名字是可编辑的),这样只需点击“提交”即可收集所有用户的名字。
-@bank_accounts.each do |account|
=form_for account do |f|
=f.text_field :first_name
上面的代码类似于
<form class="edit_bank_accounts" id="edit_bank_account_1" action="/bank_account/1" accept-charset="UTF-8" method="post">
<input type="text" value="123" name="bank_account[first_name]" id="bank_account_first_name"/>
</form>
<form class="edit_bank_accounts" id="edit_bank_account_2" action="/bank_account/2" accept-charset="UTF-8" method="post">
<input type="text" value="456" name="bank_account[first_name]" id="bank_account_first_name"/>
</form>
但我想要的是
<form class="edit_bank_accounts" id="edit_bank_account" action="/bank_account" accept-charset="UTF-8" method="post">
<input type="text" value="123" name="bank_account[1][first_name]" id="bank_account_first_name"/>
<input type="text" value="456" name="bank_account[2][first_name]" id="bank_account_first_name"/>
</form>
如何在Ruby on Rails中为表单生成bank_account[id][first_name]
?
感谢。
答案 0 :(得分:1)
在表单标记内移动循环
= form_tag '/bank_account', html: { id: 'edit_bank_accounts', class: 'edit_bank_accounts' } do
-@bank_accounts.each do |account|
= text_field_tag :bank_account_first_name, account.first_name, name: "bank_account[#{account.id}][first_name]"