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会起作用,但buton_to似乎总是被视为一个帖子。
Is there an easy way to simply replace a (non-submit) link with a button?
有一种简单的方法可以简单地用按钮替换(非提交)链接吗?
4 个解决方案
#1
70
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
你所要做的就是在你的button_to的末尾添加一个:method =>“get”,让它被视为一个链接
The button_to Way
Using a users_path
使用users_path
<%= button_to "BUTTON: link version", users_path, :method => "get" %>
The CSS Way
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 it and 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!
代码来自上面的链接。无论你选择哪种方法都应该工作正常,尽情享受!
#2
11
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'添加类',new_subject_path,类:“btn btn-primary”%>
#3
8
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" %>
#4
1
If all you want to do is make a link look like a button.
Changing link_to
to button_to
is all I needed to do.
将link_to更改为button_to就是我需要做的。
#1
70
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
你所要做的就是在你的button_to的末尾添加一个:method =>“get”,让它被视为一个链接
The button_to Way
Using a users_path
使用users_path
<%= button_to "BUTTON: link version", users_path, :method => "get" %>
The CSS Way
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 it and 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!
代码来自上面的链接。无论你选择哪种方法都应该工作正常,尽情享受!
#2
11
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'添加类',new_subject_path,类:“btn btn-primary”%>
#3
8
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" %>
#4
1
If all you want to do is make a link look like a button.
Changing link_to
to button_to
is all I needed to do.
将link_to更改为button_to就是我需要做的。