Form_for inputs and submit button in different partials

喜你入骨 提交于 2019-12-22 14:03:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!