Rails:多次使用form_for(DOM ID) [英] Rails: Using form_for multiple times (DOM ids)
问题描述
我想对同一页面中的同一模型多次使用form_for帮助器.但是输入字段使用相同的ID属性(在HTML中),因此,单击其他表单中字段的标签将在第一个表单中选择相同的输入.
I would like to use the form_for helper multiple times for the same model in the same page. But the input fields use the same ID attribute (in the HTML), so clicking on the label of a field in another form will select the same input in the first form.
除了通过:for =>"title _#{item.id}"和:id =>"title _#{item.id}"手动设置所有属性之外,还有解决方案吗?
Is there a solution besides settings all attributes manually via :for => "title_#{item.id}" and :id => "title_#{item.id}"?
使用Rails 3.0.9
Using Rails 3.0.9
推荐答案
您可以使用:namespace => 'some_unique_prefix'
选项.与:index
相比,这不会更改name
属性中使用的值.
You can use :namespace => 'some_unique_prefix'
option. In contrast to :index
, this will not change the value used in the name
attribute.
也可以使用数组,例如当您有嵌套表单或恰好具有某些公共字段的其他表单时::namespace => [@product.id, tag.id]
或:namespace => [:product, @product.id]
It's also possible to use an array, e.g. when you have nested forms or different forms that happen to have some fields in common: :namespace => [@product.id, tag.id]
or :namespace => [:product, @product.id]
这篇关于Rails:多次使用form_for(DOM ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!