I have a basic form using the form_tag helper working fine, but I want to add a cancel button, what is the syntax for doing this? I want the cancel button to appear as a button, not a link, then take the user to a different URL (indicating they don't want to submit the form).
我有一个基本的表单使用form_tag帮助工作正常,但我想添加一个取消按钮,这样做的语法是什么?我希望取消按钮显示为按钮,而不是链接,然后将用户带到不同的URL(表示他们不想提交表单)。
TY, Fred
TY,弗雷德
4 个解决方案
#1
23
If you want to clear/reset the form fields, do what dchacke suggests.
如果要清除/重置表单字段,请执行dchacke建议的操作。
However, I'd generally expect a Cancel button to not clear the form, but to take me away from the form, meaning I don't plan on submitting it.
但是,我通常希望取消按钮不会清除表单,而是将我从表单中删除,这意味着我不打算提交表单。
If you want the latter, I'd just make a link (or button) to the page you want to go to upon cancelling, such as :
如果你想要后者,我只需要在取消时为你要去的页面建立一个链接(或按钮),例如:
=link_to 'Cancel', my_page_path
Or if you want a button:
或者如果你想要一个按钮:
= button_tag "Cancel", :type => 'button'
Then add this to your controller:
然后将其添加到您的控制器:
before_filter :check_for_cancel, :only => [:create, :update]
def check_for_cancel
if params[:commit] == "Cancel"
redirect_to my_page_path
end
end
#2
22
If you mean a reset button, paste the following inside your form:
如果您的意思是重置按钮,请在表单中粘贴以下内容:
<%= button_tag "Reset", type: :reset %>
Tested it, it works fine and resets all fields in the form.
测试它,它工作正常并重置表单中的所有字段。
#3
2
Instead of a submit tag, I would suggest a <button>
tag, because the parameter :commit
might be localised, thus making it a pain to evaluate in the controller.
而不是提交标记,我会建议一个
A button_tag
can have a name/value like any other field, so you can basically create a flag with it.
button_tag可以像任何其他字段一样具有名称/值,因此您基本上可以使用它创建一个标志。
The following code is what I would use:
我将使用以下代码:
in the view:
在视图中:
<%= button_tag t('buttons.cancel'), type: "submit", name: "cancel", value: true %>
in the controller:
在控制器中:
before_filter :redirect_cancel, only: [:create, :update]
private
def redirect_cancel
redirect_to my_page_path if params[:cancel]
end
Much cleaner imho, because you rely on a flag instead of a (potentially) localised display value.
更清洁的imho,因为你依赖于标志而不是(可能)本地化的显示值。
#4
1
button_to
seems a simple way to achieve the same, but it has to be put outside of the form itself, but that can easily be fixed with css.
button_to似乎是一种实现相同的简单方法,但它必须放在表单本身之外,但可以用css轻松修复。
Also, to solve this in a more generic way (also working for editing nested resources on a resource that has not been persisted yet), you can create a separate form for the cancel that contains all the original values as hidden fields.
此外,要以更通用的方式解决此问题(还用于编辑尚未保留的资源上的嵌套资源),可以为取消创建单独的表单,其中包含所有原始值作为隐藏字段。
#1
23
If you want to clear/reset the form fields, do what dchacke suggests.
如果要清除/重置表单字段,请执行dchacke建议的操作。
However, I'd generally expect a Cancel button to not clear the form, but to take me away from the form, meaning I don't plan on submitting it.
但是,我通常希望取消按钮不会清除表单,而是将我从表单中删除,这意味着我不打算提交表单。
If you want the latter, I'd just make a link (or button) to the page you want to go to upon cancelling, such as :
如果你想要后者,我只需要在取消时为你要去的页面建立一个链接(或按钮),例如:
=link_to 'Cancel', my_page_path
Or if you want a button:
或者如果你想要一个按钮:
= button_tag "Cancel", :type => 'button'
Then add this to your controller:
然后将其添加到您的控制器:
before_filter :check_for_cancel, :only => [:create, :update]
def check_for_cancel
if params[:commit] == "Cancel"
redirect_to my_page_path
end
end
#2
22
If you mean a reset button, paste the following inside your form:
如果您的意思是重置按钮,请在表单中粘贴以下内容:
<%= button_tag "Reset", type: :reset %>
Tested it, it works fine and resets all fields in the form.
测试它,它工作正常并重置表单中的所有字段。
#3
2
Instead of a submit tag, I would suggest a <button>
tag, because the parameter :commit
might be localised, thus making it a pain to evaluate in the controller.
而不是提交标记,我会建议一个
A button_tag
can have a name/value like any other field, so you can basically create a flag with it.
button_tag可以像任何其他字段一样具有名称/值,因此您基本上可以使用它创建一个标志。
The following code is what I would use:
我将使用以下代码:
in the view:
在视图中:
<%= button_tag t('buttons.cancel'), type: "submit", name: "cancel", value: true %>
in the controller:
在控制器中:
before_filter :redirect_cancel, only: [:create, :update]
private
def redirect_cancel
redirect_to my_page_path if params[:cancel]
end
Much cleaner imho, because you rely on a flag instead of a (potentially) localised display value.
更清洁的imho,因为你依赖于标志而不是(可能)本地化的显示值。
#4
1
button_to
seems a simple way to achieve the same, but it has to be put outside of the form itself, but that can easily be fixed with css.
button_to似乎是一种实现相同的简单方法,但它必须放在表单本身之外,但可以用css轻松修复。
Also, to solve this in a more generic way (also working for editing nested resources on a resource that has not been persisted yet), you can create a separate form for the cancel that contains all the original values as hidden fields.
此外,要以更通用的方式解决此问题(还用于编辑尚未保留的资源上的嵌套资源),可以为取消创建单独的表单,其中包含所有原始值作为隐藏字段。