# More elegant solution for turning link into button?

**URL:** <https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436>\
**Category:** Rails\
**Created:** [March 2, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436 "2010-03-02T16:27:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/1 "2010-03-02T16:27:16Z")

</div>

I’d like my print link to look like a button. Its action responds to  
a GET request (as I think it should.) I wrote this helper:

`
  def button_link( label, path, *features ) # makes a link look like a
button
    linktext = " 0 # opens new window, assigns features
      linktext << "window.open('" << path << "','" << label << "'"
      features.each { |feature| linktext << ",'" << feature << "'" }
      linktext <'
    else # opens in same window
      linktext <'
    end
  end
`

Which works as expected in the view, e.g.  
`
<%= button_link( ‘Print’, url_for( … ), ‘new_window’ ) %>
`

But feels kind of hacky. Does anyone have a more elegant solution?

TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 4:33pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/2 "2010-03-02T16:33:07Z")

</div>

On Tue, Mar 2, 2010 at 7:26 AM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like my print link to look like a button. Its action responds to  
> a GET request (as I think it should.) I wrote this helper:

> But feels kind of hacky. Does anyone have a more elegant solution?

Just style a regular link; that’s what CSS is for…

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/3 "2010-03-02T16:55:57Z")

</div>

> Just style a regular link; that’s what CSS is for…
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

That would be useful if you wanted to use an image for the link. But  
what if you want the default button for the browser?  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/4 "2010-03-02T17:17:03Z")

</div>

> You don’t need to use an image to style a link to look generically like  
> a button, but yes, if you want it to look like a platform’s native button,  
> then you need to use a form element (or jump through a lot of hoops  
> to try to simulate it).
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

How would you style a link to look generically like a button without  
an image? Sorry for wandering OT into CSS land, but if the answer  
works, it cuts out some Rails code in my app. Keep in mind that I  
need the functionality of opening a new window when the user presses a  
button, which my code currently provides.

Thanks,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 5:05pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/5 "2010-03-02T17:05:54Z")

</div>

On Tue, Mar 2, 2010 at 7:54 AM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Just style a regular link; that’s what CSS is for…

> That would be useful if you wanted to use an image for the link. But  
> what if you want the default button for the browser?

You don’t need to use an image to style a link to look generically like  
a button, but yes, if you want it to look like a platform’s native  
button,  
then you need to use a form element (or jump through a lot of hoops  
to try to simulate it).

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 5:56pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/6 "2010-03-02T17:56:17Z")

</div>

On Tue, Mar 2, 2010 at 8:15 AM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How would you style a link to look generically like a button without  
> an image? Sorry for wandering OT into CSS land, but if the answer  
> works, it cuts out some Rails code in my app. Keep in mind that I  
> need the functionality of opening a new window when the user presses a  
> button, which my code currently provides.

A quick example: [http://webtuitive.com/samples/button.html](http://webtuitive.com/samples/button.html)

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/7 "2010-03-02T18:49:33Z")

</div>

On Tue, Mar 2, 2010 at 9:42 AM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Appreciate it, but that’s a heck of a lot of CSS code to get something  
> that approximates the look of a default button 😉

Are you kidding? It’s utterly trivial. And you don’t even have to use  
most of that if you don’t care about the hover/active behavior.

And I would certainly prefer it to creating a form element needlessly  
for a simple link. But YMMV.

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 2, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/8 "2010-03-02T18:43:42Z")

</div>

> A quick example: [http://webtuitive.com/samples/button.html](http://webtuitive.com/samples/button.html)
> 
> –  
> Hassan S. ------------------------ removed\_email\_address@domain.invalid  
> twitter: @hassan

Appreciate it, but that’s a heck of a lot of CSS code to get something  
that approximates the look of a default button 😉

Thanks,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [March 4, 2010, 4:34am UTC](https://www.ruby-forum.com/t/more-elegant-solution-for-turning-link-into-button/184436/9 "2010-03-04T04:34:58Z")

</div>

> Appreciate it, but that’s a heck of a lot of CSS code to get something  
> that approximates the look of a default button 😉
> 
> Thanks,  
> Craig

Hassan is right that its trivial css implementation.

If you feel like learning SASS & Compass, there is the fancy buttons  
mixin that gives very , well fancy, buttons  
[http://brandonmathis.com/blog/2009/11/19/fancy-buttons-are-here/](http://brandonmathis.com/blog/2009/11/19/fancy-buttons-are-here/)

Brian
