Ruby-on-rails 在 Rails3+ 中禁用 link_to 标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6925286/
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
Disable link_to tag in Rails3+
提问by lamrin
I used the following code:
我使用了以下代码:
<%= link_to image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
I want to disable this link and image, so I added :disabled=>trueto the code, but it's not disabling. Why not, and how do I disable them?
我想禁用此链接和图像,所以我添加:disabled=>true到代码中,但它并没有禁用。为什么不,我如何禁用它们?
回答by Claudio Shigueo Watanabe
I'm not sure what @lamrin wanted with this question, but I suppose that it is something like this:
我不确定@lamrin 这个问题想要什么,但我想它是这样的:
<%= link_to_if condition?, image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
With this code above, the image would have a link if the condition? is true
使用上面的代码,如果条件成立,图像将有一个链接?是真的
In my case this code below worked (a more complicated example):
在我的情况下,下面的代码有效(一个更复杂的例子):
link_to_unless disabled, (content_tag :div, "", :class => "vote " + vote_class, :title => title), resource_user_path({ :id => resuser.id, :resource_user => {:id => resuser.id, :resource_id => resource_id, :user_id => current_user_id, :vote => vote_value}}), :remote => true, :method => http_method
This link may also help with this approach:
此链接也可能对这种方法有所帮助:
http://railskey.wordpress.com/2012/07/19/rails-link_to-link_to_if-and-link_to_unless/
http://railskey.wordpress.com/2012/07/19/rails-link_to-link_to_if-and-link_to_unless/
回答by Gaurav Gupta
Unlike buttons, hyperlinks cannot be "disabled". You can do the following though, assuming you have jQuery included on your pages:
与按钮不同,超链接不能被“禁用”。不过,您可以执行以下操作,假设您的页面中包含 jQuery:
<%=link_to image_tag("edit.png", :alt=>"Edit"), edit_user_path(user), :id => "mylink" %>
Add the following Javascript to your page:
将以下 Javascript 添加到您的页面:
$('#mylink').click(function(e){
e.preventDefault();
});
回答by Laney Stroup
In answer to your question, there is no :disabled option for the link_to helper in Rails, and it is not a valid attribute for a elements either. I believe the reason people tend to get confused with this in Rails is that ":disabled => true" does work IF you are using Bootstrap. So to fix this issue you can either follow Gupta's approach, or just add Bootstrap (which will give you some default CSS as well, so people don't get frustrated trying to click the link)!
在回答您的问题时,Rails 中的 link_to 助手没有 :disabled 选项,并且它也不是元素的有效属性。我相信人们倾向于在 Rails 中对此感到困惑的原因是“:disabled => true”在您使用 Bootstrap 时确实有效。因此,要解决此问题,您可以遵循 Gupta 的方法,也可以仅添加 Bootstrap(这也会为您提供一些默认 CSS,因此人们在尝试单击链接时不会感到沮丧)!
Re: link_to method in rails: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
回复:rails 中的 link_to 方法:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to )
Re: the "disabled" attribute on a elements: Is 'disabled' a valid attribute for an anchor tag
回复:元素上的“禁用”属性:“禁用”是锚标记的有效属性
Re: Bootstrap "disabled" class or attribute with bootstrap: http://getbootstrap.com/css/#anchor-element-1
回复:带有引导程序的引导程序“禁用”类或属性:http: //getbootstrap.com/css/#anchor-element-1
回答by naren
1)One solution is to render just image_tag when you do not want link and use link_to when u want link to be click enabled. you can use instance variables to control what to render.
1)一种解决方案是当您不想要链接时仅渲染 image_tag 并在您想要启用链接时使用 link_to 。您可以使用实例变量来控制要呈现的内容。
2) or use Javascript as suggested.
2)或按照建议使用Javascript。
Use 2 if you want to dynamically do it.
如果要动态执行,请使用 2。
回答by Meduza
You may use conditional link_to:
您可以使用条件 link_to:
<%=
link_to_if(@current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
link_to(@current_user.login, { :controller => "accounts", :action => "show", :id => @current_user })
end
%>

