Ruby on Rails:如何在form_tag中实现“取消"按钮 [英] Ruby on Rails : how to implement Cancel button in form_tag
问题描述
我有一个使用form_tag帮助器的基本表单,但工作正常,但是我想添加一个取消按钮,这样做的语法是什么?我希望取消按钮显示为按钮,而不是链接,然后将用户带到其他URL(指示他们不想提交表单).
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).
TY, 弗雷德
推荐答案
如果要清除/重置表单字段,请执行dchacke的建议.
If you want to clear/reset the form fields, do what dchacke suggests.
但是,我通常希望取消按钮不会清除表单,而是让我离开表单,这意味着我不打算提交表单.
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
这篇关于Ruby on Rails:如何在form_tag中实现“取消"按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!