如何使用 link_to Ruby on rails 添加确认消息

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

How to add confirm message with link_to Ruby on rails

rubyruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2link-to

提问by Tini

I wanted to add confirmation message on link_to function with Ruby.

我想在使用 Ruby 的 link_to 函数上添加确认消息。

= link_to 'Reset message', :action=>'reset' ,:confirm=>'Are you sure?'

Any ideas why it's not working?

任何想法为什么它不起作用?

采纳答案by dpaluy

First, you should verify that your layout have jquery_ujs. Best practice to do it by including it in your main application.js:

首先,您应该验证您的布局是否具有jquery_ujs。最佳做法是将其包含在您的主application.js 中

//= require jquery_ujs

Check that you included application.jsin your layout:

检查您的布局中是否包含application.js

= javascript_include_tag :application

While, in development mode, view your source html and verify jquery_ujs.jsexists.

而在开发模式下,查看源 html 并验证jquery_ujs.js是否存在。

Run your server and verify your link tag has data-confirmvalue, for example:

运行您的服务器并验证您的链接标签具有数据确认值,例如:

<a href="/articles/1" data-confirm="Are you sure?" data-method="delete">

If all those steps are correct, everything should work!

如果所有这些步骤都是正确的,一切都应该有效!

Note:check this RailsCast http://railscasts.com/episodes/136-jquery-ajax-revised

注意:检查这个 RailsCast http://railscasts.com/episodes/136-jquery-ajax-revised

回答by James

I might be mistaken but you don't specify a controller along with the :actionoption. Have you tried the following? Assuming you have a messagesresource configured in your route:

我可能弄错了,但您没有指定控制器和:action选项。您是否尝试过以下方法?假设您messages在路由中配置了一个资源:

link_to 'Reset', message_path(@message), :confirm => 'Are you sure?'

EDIT: Above is deprecated. Rails 4.0 now accepts the prompt as a data attribute. See the doc here(Thanks @Ricky).

编辑:以上已弃用。Rails 4.0 现在接受提示作为数据属性。请参阅此处的文档(感谢@Ricky)。

link_to 'Reset', message_path(@message), :data => {:confirm => 'Are you sure?'}

回答by Lukasz Muzyka

Can't remember how this was done in Rails 3, but in Rails 4 you can simply:

不记得这是如何在 Rails 3 中完成的,但在 Rails 4 中,您可以简单地:

<%= link_to 'Reset message', { controller: 'your_controller', action: 'reset' }, data: {confirm: 'Are you sure?'} %>

回答by diego

<%= link_to 'Reset Message', data: {confirm:"Are you sure?"} %>

remember to add the path, between 'reset message' and data

记得在“重置消息”和数据之间添加路径

回答by Ankit Wadhwana

<%= link_to "Delete this article", article_path(article), method: :delete,
                    data: { confirm: "Are you sure you want to delete the 
                    article?"}, class: "btn btn-xs btn-danger" %>

A button link where article_pathis the prefix and (article)is passing the idwhich is required by the method: :deletemethod. The later part of the codes adds a confirmation msg.

一个按钮链接,其中article_path是前缀并(article)传递方法id所需的method: :delete。代码的后面部分添加了确认信息。

回答by rubyprince

Try this:

尝试这个:

= link_to 'Reset message', {:action=>'reset'}, :confirm=>'Are you sure?'

or to be more clear

或者更清楚

= link_to('Reset message', {:action=>'reset'}, {:confirm=>'Are you sure?'})

Refer http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

参考http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

You will see that there are 3 parameters, when you are giving url as options like {:action => ..., :controller => ...}

当您将 url 作为选项提供时,您将看到有 3 个参数 {:action => ..., :controller => ...}

link_to(body, url_options = {}, html_options = {})

In ruby, if the last parameter in a function call is a hash, you need not wrap it in {}characters (in other words, you can omit that in case, if the hash is the last parameter), so the code you have provided will be interpreted as a function call with only 2 parameters, 'Reset message'string and {:action=>'reset', :confirm=>'Are you sure?'}hash and the :confirm=>'Are you sure?'will be interpreted as a url_optioninstead of a html_option

在 ruby​​ 中,如果函数调用中的最后一个参数是散列,则不需要将其包装在{}字符中(换句话说,如果散列是最后一个参数,则可以省略),因此您提供的代码将被解释为只有 2 个参数、'Reset message'字符串和{:action=>'reset', :confirm=>'Are you sure?'}哈希的函数调用,并且:confirm=>'Are you sure?'将被解释为 aurl_option而不是 ahtml_option

回答by Ryosuke Hujisawa

Somehow does not work those code only Safari browser So I was involved button...

不知何故,这些代码仅适用于 Safari 浏览器所以我参与了按钮...

<%= button_to('', delete_path(), method: "delete", data: { confirm: 'Are you sure?', disable_with: 'loading...' }) %>

回答by V K Singh

First, we need to understand what Js package respond to this kind of alerts in rails application. So for this, jquery_ujs package is reponsible for showing the alerts in rails.

首先,我们需要了解 Rails 应用程序中哪些 Js 包响应这种警报。因此,为此, jquery_ujs 包负责在 rails 中显示警报。

So you must have jquery & jquery_ujs in your application.js file.

所以你的 application.js 文件中必须有 jquery & jquery_ujs。

//= require jquery
//= require jquery_ujs

Now, we need to confirm, that application.js file is included in your required layout or not. By default layout file remains in application.html.erb in layout folder of views.

现在,我们需要确认 application.js 文件是否包含在您所需的布局中。默认情况下,布局文件保留在视图布局文件夹中的 application.html.erb 中。

<%= javascript_include_tag 'application' %>

Next the link should have data-confirm & data-method attributes as

接下来链接应该具有数据确认和数据方法属性作为

<a href="/message/1/reset" data-method="delete" data-confirm="Are you sure?">

In erb, this can be written as,

在 erb 中,这可以写成,

= link_to 'Reset', message_path(@message), data: {method: 'delete', confirm: 'Are you sure?'}

This should work if everything is aligned in same fashion.

如果一切都以相同的方式对齐,这应该有效。

回答by Sumit Munot

Look at your javascript_include_tag and it should work fine:

看看你的 javascript_include_tag ,它应该可以正常工作:

<%= link_to("Reset message", :method => :reset, :class => 'action', :confirm => 'Are you sure?') %>

回答by Sachin Singh