Ruby-on-rails button_to 没有提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6727251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
button_to without submit
提问by jojo
Can I have a button in side the form, but do not have the type=submit?
我可以在表单旁边有一个按钮,但没有type=submit吗?
I am doing something special. I have a form,
我正在做一些特别的事情。我有一个表格,
<%= form_for(@user) do |f| %>
and this form will be rendered based on a value in a session.
并且此表单将根据会话中的值呈现。
In side the form, I have f.submitalready and I want to have another button, cancel button, to invoke a method from controller to change the session value if user want to cancel the input.
在表单旁边,我f.submit已经有了另一个按钮,即取消按钮,如果用户想要取消输入,它可以从控制器调用一个方法来更改会话值。
But if I use button_to, the type of the button will be "submit", when I click on the button, it will submit the whole form. If some of the value are not valid, it will complain. It does not work as the "cancel" button that I expected.
但是如果我使用button_to,按钮的类型将是“提交”,当我点击按钮时,它会提交整个表单。如果某些值无效,它会抱怨。它不像我期望的“取消”按钮那样工作。
So can I have a button that does not submit the form?
那么我可以有一个不提交表单的按钮吗?
I tried to use submit_tag, but the buttom dosn`t work for me...
我尝试使用 submit_tag,但按钮对我不起作用...
<%= submit_tag 'Cancel Edit', :type => 'button', :controller => 'my_account', :action=>'cancel_edit' %>
采纳答案by mu is too short
The button_tohelperactually generates a whole form:
该button_to助手实际上产生一个整体的形式:
Generates a form containing a single button that submits to the URL created by the set of
options.
生成一个包含单个按钮的表单,该按钮提交到由
options.
So that's definitely not what you want. I think you just want to generate a simple <input type="button">element in your HTML. However, there is no button_tagin FormTagHelperbut there is a submit_tagthat you can hand a :typeoption to and that will:
所以这绝对不是你想要的。我认为您只想<input type="button">在 HTML 中生成一个简单的元素。但是,没有button_taginFormTagHelper但有一个submit_tag您可以:type选择的选项,它将:
Any other key creates standard HTML options for the tag.
任何其他键都会为标签创建标准 HTML 选项。
So try this:
所以试试这个:
submit_tag 'Pancakes', :type => 'button'
where "Pancakes" would be your real label.
“煎饼”将是您真正的标签。
回答by Beena Shetty
Try the following:
请尝试以下操作:
<%= button_tag "Cancel", :type => 'button', :class => "subBtn", :onclick => "location.href = '#{list_admin_users_admin_index_path()}'" %>
回答by Alex Kojin
You can use helper similar to f.submit:
您可以使用类似于f.submit以下内容的帮助程序:
<%= f.button 'Save' %>
<%= f.button 'Save' %>

