rails 3: display link as button?
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.
Is there an easy way to simply replace a (non-submit) link with a button?
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
The button_to Way
Using a 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
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!
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"%>
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>This is a button</button>".html_safe, some_path, :id => "button_id", :class => "button_class" %>
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.
精彩评论