问题
Is there a way to split an a form (using form_for) across two partials? I want to have text boxes in one partial, and my submit button in another.
回答1:
You can do that as follows:
# _post_form.html.erb
<%= form_for(@post) do |f| %>
<% @form = f%>
<%= render 'form_fields'%>
<%= render 'form_actions'%>
<% end %>
# _form_fields.html.erb
<div class="field">
<%= @form.label :name %><br />
<%= @form.text_field :name %>
</div>
<div class="field">
<%= @form.label :title %><br />
<%= @form.text_field :title %>
</div>
<div class="field">
<%= @form.label :content %><br />
<%= @form.text_area :content %>
</div>
# _form_actions.html.erb
<div class="actions">
<%= @form.submit %>
</div>
来源:https://stackoverflow.com/questions/10337647/form-for-inputs-and-submit-button-in-different-partials