Ruby on Rails:如何在 form_tag 中实现取消按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14310468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:17:33  来源:igfitidea点击:

Ruby on Rails : how to implement Cancel button in form_tag

ruby-on-railsrubyforms

提问by fred basset

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

泰,弗雷德

回答by 99miles

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

回答by dchacke

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.

测试它,它工作正常并重置表单中的所有字段。

回答by Overbryd

Instead of a submit tag, I would suggest a <button>tag, because the parameter :commitmight be localised, thus making it a pain to evaluate in the controller.

我建议使用<button>标签而不是提交标签,因为参数:commit可能是本地化的,因此在控制器中进行评估变得很痛苦。

A button_tagcan have a name/value like any other field, so you can basically create a flag with it.

Abutton_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.

恕我直言,更干净,因为您依赖于标志而不是(潜在的)本地化显示值。

回答by bert bruynooghe

button_toseems 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.

此外,为了以更通用的方式解决此问题(也可用于编辑尚未持久化的资源上的嵌套资源),您可以为取消创建一个单独的表单,其中包含所有原始值作为隐藏字段。