Preloading form data
I'm using the technique Ryan Bates illustrated in Railscasts Episode 197 s开发者_如何学JAVAo 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.
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.
Am I thinking about this wrong?
Yes, you're thinking about it wrong ;-)
Take this form for example:
<% 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
is an iterator; so all your controller has to do is:
@survey.questions.build(:content => "Prepared question")
and it will show up on the page.
Generate the text fields like you normally would, in the Rails view.
精彩评论