本文介绍了Form_for 输入和提交按钮在不同的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将一个表单(使用 form_for)分成两个部分?我想在一个部分中有文本框,在另一个中有我的提交按钮.

解决方案

您可以这样做:

# _post_form.html.erb<%= form_for(@post) 做 |f|%><%@form = f%><%=render 'form_fields'%><%=render 'form_actions'%><%结束%># _form_fields.html.erb<div class="field"><%= @form.label :name %><br/>

<div class="field"><%= @form.label :title %><br/><%= @form.text_field :title %>

<div class="field"><%= @form.label :content %><br/><%= @form.text_area :content %>

# _form_actions.html.erb<div class="actions"><%[email protected] %>

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.

解决方案

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>

这篇关于Form_for 输入和提交按钮在不同的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:06