2在表单中提交按钮 [英] 2 submit buttons in a form

查看:134
本文介绍了2在表单中提交按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于表单的问题。我有一个相当标准的表格,用标题和正文保存了一篇文章(在我的应用中称为eReport)。该表还具有布尔型的发布字段。保存的eReport仅在公共站点上显示,如果此字段设置为true,则默认为false。



而不是默认复选框,我希望显示表单末尾的两个按钮:立即发布按钮和另存为草稿按钮。如果用户按下前者,则发布的字段将设置为true。如果后者,那么是假的。在PHP中,我曾使用不同名称值显示2个提交字段,然后使用if / else语句处理输入以确定要构建的正确SQL查询。在Rails中,我假设我会将这个逻辑放在控制器中,在适当的操作下,但我不知道如何操纵按钮的名称或ID值。



为了记录,我使用Formtastic,但如果有人能告诉我如何使用默认的Rails窗体标签来做到这一点,那也没关系。以下是我的表单的代码:

 <%semantic_form_for @ereport do | form | %GT; 

<%form.inputs do%>
<%= form.input:title%>
<%= form.input:body%>
<%end%>
<%form.buttons do%>
<%= form.commit_button:label => 立即发布%>
<%= form.commit_button:label => 另存为草稿%>
<%end%>
<%end%>

预先感谢您的帮助! 解决方案

我不知道formtastic,但使用默认的rails表单生成器,你可以这样做:

 <%= form.submit用选项A保存,:name => save_option_a%> 
<%= form.submit用选项B保存,:name => save_option_b%>

然后在控制器中,您可以在参数中选择这些:

  if params [:save_option_a] 
#do stuff
end


I have a question about forms. I have a fairly standard form that saves a post (called an eReport in my app) with a title and body. The table also has a "published" field, which is boolean. The saved eReport only shows on the public site if this field is set to true, with false being the default.

Rather than the default check box, I would like to display two buttons at the end of the form: a "Publish Now" button and a "Save as Draft" button. If the user presses the former, the published field would be set to true. If the latter, then false. In PHP, I used to display 2 submit fields with different name values, then handle the input with an if/else statement to determine the proper SQL query to build. In Rails, I'm assuming I would place this logic in the controller, under the appropriate action, but I'm not sure how to manipulate the name or id values of buttons.

For the record, I'm using Formtastic, but if someone could show me how to do this with the default Rails form tags, that's OK too. Here's the code for my form as it stands right now:

<% semantic_form_for @ereport do |form| %>

  <% form.inputs do %>
    <%= form.input :title %>
    <%= form.input :body %>
  <% end %>
  <% form.buttons do %>
    <%= form.commit_button :label => "Publish Now" %>
<%= form.commit_button :label => "Save as Draft" %>
  <% end %>
<% end %>

Thanks in advance for the help!

解决方案

I don't know about formtastic, but with the default rails form builder, you could do it like this:

<%= form.submit "Save with option A", :name => "save_option_a" %>
<%= form.submit "Save with option B", :name => "save_option_b" %>

Then in the controller, you can pick those up in params:

if params[:save_option_a]
  # do stuff
end

这篇关于2在表单中提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆