2 submit buttons in a form

馋奶兔 提交于 2019-11-30 09:20:06

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

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

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.

which was obtained by 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'...

and retrieve them in your controller

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

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

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