fields_for 和 jQuery 手风琴兼容性
我实际上只是从 Railsforum 复制了他在 09 年 5 月提出的一个老问题,唯一的答复是作者自己的不优雅的修复。我想知道是否有人知道更好的方法?否则我最终将使用 javascript 来移动隐藏字段,就像他那样。
这是原来的问题:
你好! 感谢您对此主题的任何好建议:
对于 has_many 关联,是否可以覆盖 Rails 在 fields_for 中自动生成隐藏 ID 字段的功能?我想生成自己的,以便我可以选择事物的去向。
原因如下:
我试图在一个看起来有点像这样的表单周围放置一个 jQuery-ui Accordion 小部件:
<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>
简化后的模型如下:
class Diagram < ActiveRecord::Base
has_many :elements
accepts_nested_attributes_for :elements
end
我认为我已经掌握了模型/控制器的内容,因为我可以随心所欲地进行增删改查。但手风琴出来时却很奇怪。
jQuery UI/Accordion 小部件以以下形式查找 html:
<div id="accordion">
<a href="#">First header</a>
<div>First content</div>
<a href="#">Second header</a>
<div>Second content</div>
</div>
这似乎是用 Rails 完成的一件容易的事情,但我真的很窒息。 选项卡显示但倾斜,最后我意识到这是因为rails自动为fields_for中的每个对象添加的hidden_field。我最终得到的结果如下所示:
<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>
...这样隐藏字段就会被视为手风琴结构的一部分。我想将 ID 字段移动到 DIV 标签内。有什么想法吗?
再次感谢, 泰勒
我在这里遇到了与泰勒完全相同的问题。 fields_for 生成的隐藏字段弄乱了手风琴用户界面,目前除了用 JavaScript 移动它们之外,我看不到改变它们位置的方法。
我将在 Railsforum 上发布一个链接,希望泰勒也能从你们的集体智慧中受益。
问候,
凯文。
I'm actually just copying an old question from railsforum as he asked it back in May '09 and the only reply is the authors own inelegant fix. I wondered if anybody knows of a better way? Otherwise I'm going to end up using javascript to move the hidden field about as he has.
Here's the original question:
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
I'm having exactly the same issue as Tyler here. The hidden fields generated by fields_for are messing up the accordion UI and at the moment I can't see a way to change the position of them except moving them with javascript.
I shall post a link to this back on railsforum so that hopefully Tyler will receive any benefit of your collective wisdom as well.
Regards,
Kevin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Rails 2.3.5 开始,您可以将
f.hidden_field(:id)
放置在您希望存在的位置,Rails 将填写详细信息。有关此更改的票证可在此处获取。
Since Rails 2.3.5 you can place a
f.hidden_field(:id)
where you would like it to exist, and Rails will fill out the details.The ticket regarding this change is available here.