Ruby-on-rails rails 3:将链接显示为按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6247491/
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
rails 3: display link as button?
提问by jpw
On some non-form pages, I have a few links that would look better as a button than a hyperlink...
在一些非表单页面上,我有一些链接看起来像按钮而不是超链接......
I thought button_to instead of link_to would work, but buton_to seems to always be treated as a post.
我认为 button_to 而不是 link_to 会起作用,但 button_to 似乎总是被视为帖子。
Is there an easy way to simply replace a (non-submit) link with a button?
有没有一种简单的方法可以简单地用按钮替换(非提交)链接?
回答by Paul Kaplan
All you have to do is add a :method => "get"to the end of your button_to to get it to be treated like a link
您所要做的就是:method => "get"在 button_to 的末尾添加一个,以使其被视为链接
The button_to Way
button_to 方式
Using a users_path
用一个 users_path
<%= button_to "BUTTON: link version", users_path, :method => "get" %>
The CSS Way
CSS方式
Or instead of actually inserting a form into your html (which is what button_to actually does) you could go with the cleaner (from a web design perspective) method and actually just style the link in a way to make it look like a button This has several benefits
或者,您可以使用更简洁的方法(从网页设计的角度来看),而不是实际将表单插入到您的 html 中(这就是 button_to 实际所做的),并且实际上只是以某种方式设置链接的样式,使其看起来像一个按钮 这有几个好处
Here is a great article on how to do itand here is a little snippet of that code, really just depends on playing around with the border, padding and background image
这是一篇关于如何做到这一点的很棒的文章,这是该代码的一小段,实际上只取决于使用边框,填充和背景图像
a.button {
background: transparent url('bg_button_a.gif') no-repeat scroll top right;
color: #444;
display: block;
float: left;
font: normal 12px arial, sans-serif;
height: 24px;
margin-right: 6px;
padding-right: 18px; /* sliding doors padding */
text-decoration: none;
}
The code comes from the link above. Whichever method you choose should work fine, enjoy!
代码来自上面的链接。无论您选择哪种方法都应该可以正常工作,享受吧!
回答by Dhruv Joshi
I think all you want here is the just view. and if that is the case then you can just add a class to the class object in link
我认为你在这里想要的只是公正的看法。如果是这种情况,那么您只需将一个类添加到链接中的类对象
<%= link_to 'Add Class', new_subject_path, class: "btn btn-primary"%>
<%= link_to 'Add Class', new_subject_path, class: "btn btn-primary"%>
回答by Robert
I recently had to do this because my form was acting differently for link_to than it was with button_to and I needed the functionality the link provided. The best solution I found does not use CSS at all but instead calls .html_safe to escape the html in the ruby code and properly display the link as a button. What you want to do is this:
我最近不得不这样做,因为我的表单对 link_to 的行为与对 button_to 的行为不同,我需要链接提供的功能。我发现的最佳解决方案根本不使用 CSS,而是调用 .html_safe 来转义 ruby 代码中的 html 并将链接正确显示为按钮。你想要做的是:
<%= link_to "<button>This is a button</button>".html_safe, some_path, :id => "button_id", :class => "button_class" %>
回答by jasonleonhard
If all you want to do is make a link look like a button.
如果您只想让链接看起来像一个按钮。
Changing link_toto button_tois all I needed to do.
更改link_to为button_to我需要做的所有事情。

