Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Add style/image to button_to

I'm developing in rails right now and I was wondering if there are any easy ways to add some style to the button_to control.

Can you add styling to the

 <%= submit_tag 'Log in' %>

or

 <%= button_to "Show Me", {:controller => 'personal', :action => "add" } %>

It would be great to change the color....But brownie point if someone can tell me how to make it an image

like image 610
ChrisWesAllen Avatar asked Mar 08 '10 01:03

ChrisWesAllen


2 Answers

Since you're using an image, there is no reason to use button_to instead of link_to, "button look" will be lost to the user. You can create an image with a link like so:

<%= link_to image_tag("rails.png"), {:controller => 'foo', :action => "bar" } %>

If, for some reason, you need to use button_to, you can give it a CSS class and apply some styles via that:

 <%= button_to "Show Me", {:controller => 'personal', :action => "add" }, {:class => "buttonTo" } %>
like image 123
Mike Trpcic Avatar answered Oct 14 '22 20:10

Mike Trpcic


In addittion to Mike's very fine pointer to using a pre-defined :class, you can also go for :style and define the CSS inline. Helps a great deal if you want to define your style on the fly:

<%= button_to "Show Me", {:controller => 'personal', :action => "add" }, {:style => "background: #{obj.colourname}" } %>

(With obj being some model instance that stores some HTML colour value at the colourname attribute.)

like image 33
Windowlicker Avatar answered Oct 14 '22 18:10

Windowlicker