预装表格数据 [英] Preloading form data
问题描述
我正在使用Railscasts第197集中介绍的Ryan Bates技术,以便随着用户向表单中添加更多项目,表单可以增长".一切都很好,但是我需要在提供初始项目作为URL的一部分的同时调用页面.
I'm using the technique Ryan Bates illustrated in Railscasts Episode 197 so that the form can "grow" as the user adds more items to the form. That's all working great, but I need to call the page while supplying an initial item as a part of the URL.
由于Ryan的技术是让用户单击添加"按钮以调用jQuery函数以将字段添加到表单中,因此,我有点阻碍了如何将信息从控制器传递到jQuery进行调用和预填充-这些新字段.
Since Ryan's technique has the user click an Add button to invoke a jQuery function to add fields to the form, I'm a little stymied how to pass information from the controller to jQuery to invoke -- and prefill -- one of those new fields.
我在想这个错误吗?
推荐答案
是的,您正在考虑的是错误的;-)
Yes, you're thinking about it wrong ;-)
以这种形式为例:
<% form_for @survey do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<% f.fields_for :questions do |builder| %>
<%= render 'question_fields', :f => builder %>
<% end %>
<p><%= link_to_add_fields "Add Question", f, :questions %>
<p><%= f.submit "Submit" %></p>
<% end %>
fields_for
是一个迭代器;所以您的控制器要做的就是:
fields_for
is an iterator; so all your controller has to do is:
@survey.questions.build(:content => "Prepared question")
,它将显示在页面上.
这篇关于预装表格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!