Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
909 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Keeps forms out of your HTML, when they really shouldn't be there Keeps your erb clean and normal, no surprises Awesomely flexible, you can make them look the way you want

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...