2在表单中提交按钮

时间:2022-11-24 20:15:37

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.

我对表格有疑问。我有一个相当标准的表单,用标题和正文保存一个帖子(在我的应用程序中称为eReport)。该表还有一个“已发布”字段,它是布尔值。保存的eReport仅在公共站点上显示此字段是否设置为true,false为默认值。

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.

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

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:

为了记录,我正在使用Formtastic,但如果有人可以告诉我如何使用默认的Rails表单标签执行此操作,那也没关系。这是我现在的表格的代码:

<% 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!

先谢谢您的帮助!

3 个解决方案

#1


20  

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

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

<%= 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:

然后在控制器中,你可以用params选择那些:

if params[:save_option_a]
  # do stuff
end

#2


1  

in addition to @iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)

除了@iddlefingers的回答,这里是应用程序日志的视图(由于解释目的而删除一些无用的参数)

Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}

where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.

我们可以看到comentar是参数的名称,而“Confirmar”是它的值,也就是按钮的文本。

which was obtained by submit_tag "Confirmar", :name => 'comentar'

这是由submit_tag“Confirmar”获得的,:name =>'comentar'

So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...

所以一般情况下你可以(如果你想减少你正在使用的params的数量)几个submit_tag“onevalue”,:name =>'SAMEname',submit_tag“othervalue”,:name =>'SAMEname'...

and retrieve them in your controller

并在您的控制器中检索它们

if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end

#3


0  

I think you need to use jQuery. You can bind the button click event and submit the form for specified location.

我认为你需要使用jQuery。您可以绑定按钮单击事件并提交指定位置的表单。

#1


20  

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

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

<%= 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:

然后在控制器中,你可以用params选择那些:

if params[:save_option_a]
  # do stuff
end

#2


1  

in addition to @iddlefingers answer, here is a view of the log of the application (striping some useless params due to explanation purposes)

除了@iddlefingers的回答,这里是应用程序日志的视图(由于解释目的而删除一些无用的参数)

Parameters: {"utf8"=>"✓", ..., "comentar"=>"Confirmar"}

where we can see that comentar is the name of the parameter, and "Confirmar" is it's value, which is the button's text too.

我们可以看到comentar是参数的名称,而“Confirmar”是它的值,也就是按钮的文本。

which was obtained by submit_tag "Confirmar", :name => 'comentar'

这是由submit_tag“Confirmar”获得的,:name =>'comentar'

So in general you could have (if you want to reduce the number of params you are working with) several submit_tag "onevalue", :name => 'SAMEname', submit_tag "othervalue", :name => 'SAMEname'...

所以一般情况下你可以(如果你想减少你正在使用的params的数量)几个submit_tag“onevalue”,:name =>'SAMEname',submit_tag“othervalue”,:name =>'SAMEname'...

and retrieve them in your controller

并在您的控制器中检索它们

if params[:SAMEname] == "onevalue"
# do stuff
elsif params[:SAMEname] == "othervalue"
#do different stuff
end

#3


0  

I think you need to use jQuery. You can bind the button click event and submit the form for specified location.

我认为你需要使用jQuery。您可以绑定按钮单击事件并提交指定位置的表单。