我实际上只是从railsforum复制了他在09年5月提出的一个老问题,唯一的答复是作者自己的不优雅的修复。我想知道是否有人知道更好的方法?否则我最终将使用 javascript 来移动隐藏字段,就像他那样。
这是原来的问题:
<小时/>Hi there! Grateful for any good advice on this topic:
Is it possible to override Rails' automatic generation of hidden ID fields in the fields_for for a has_many association? I would like to generate my own so that I can choose where the thing goes.
Here's why:
I am trying to put a jQuery-ui Accordion widget around a form that looks a bit like this:
<div id="diagram_elements_form">
<% diagram_form.fields_for :elements do |element_fields| %>
<% link_to "#" do %>
<%= element_fields.object.name %>
<% end %>
<%= render :partial => "edit_element_base", :locals => { :f => element_fields } %>
<% end %>
</div>
the model, simplified, looks like:
class Diagram < ActiveRecord::Base
has_many :elements
accepts_nested_attributes_for :elements
end
I think I've got the model/controller stuff right because I can CRUD to my heart's content. But the accordion comes out all screwy.
the jQuery UI/Accordion widget looks for html in the form of:
<div id="accordion">
<a href="#">First header</a>
<div>First content</div>
<a href="#">Second header</a>
<div>Second content</div>
</div>
This seems like an easy thing to accomplish with rails, but I am really choking. The tabs show up but are skewed, and finally I realized it's because of the hidden_field that rails automatically adds for each object in the fields_for. What I wind up with looks like this:
<div id="accordion">
<input type="hidden" name="...id" value="2" />
<a href="#">First header</a>
<div>First content</div>
<input type="hidden" name="...id" value="3" />
<a href="#">Second header</a>
<div>Second content</div>
</div>
<小时/>... Such that the hidden fields are getting taken as part of the accordion structure. I would like to move the ID fields inside of the DIV tags. Any ideas?
thanks again, Tyler
我在这里遇到了与泰勒完全相同的问题。 fields_for 生成的隐藏字段弄乱了 Accordion 用户界面,目前除了用 JavaScript 移动它们之外,我看不到改变它们位置的方法。
我将在 Railsforum 上发布一个链接,以便泰勒也能从你们的集体智慧中受益。
问候,
凯文。
最佳答案
从 Rails 2.3.5 开始,您可以将 f.hidden_field(:id)
放置在您希望存在的位置,Rails 将填写详细信息。
有关此更改的票证是 available here .
关于jquery - fields_for 和 jQuery Accordion 兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3199632/