33

I must change a link_to tag color without using a CSS class, how can I do? I've tried something such as

<%= link_to item.description, {}, {:style=>'color:#FFFFFF;', :class => "css_class"} %>

but it doesn't works on ruby 1.9.2 and rails 3.1

AnkitG
  • 6,438
  • 7
  • 44
  • 72
Marco
  • 10,283
  • 4
  • 23
  • 22

8 Answers8

38

How about

<%= link_to item.description, nil, {:style=>'color:#FFFFFF;', :class => "css_class"} %>

...or...

<%= link_to item.description, '#', {:style=>'color:#FFFFFF;', :class => "css_class"} %>
Jeff
  • 13,943
  • 11
  • 55
  • 103
ksol
  • 11,835
  • 5
  • 37
  • 64
19

This should work with Rails 3

link_to item.description, :style=> 'color:#FFFFFF;', :class => 'css_class'

With the new syntax in rails 4, it becomes

link_to item.description, style: 'color:#FFFFFF;', class: 'css_class'
Onur Kucukkece
  • 1,708
  • 1
  • 20
  • 46
9

I want to update this topic, because in this time, the syntax is different. In rails 4+, the correct syntax is:

<%= link_to TEXT, URL, class: 'css_class', style: 'color:#FFFFFF' %>
Diego Somar
  • 941
  • 1
  • 11
  • 28
7

You can try link_to item.description, {}, {:style => 'color: #FFFFFF'} is ok.

To color your links you have to set more then color:

a:link { 
  color: #333333;
}
a:visited { 
  color: #FFFFFF;
}
a:hover { 
  color: #CCCCCC;
}
a:active { 
  color: #333333;
}

I recommend to use a css class for this.

tonymarschall
  • 3,862
  • 3
  • 29
  • 52
  • To set a class you have to set ...` :class => 'nameOfClass'` – tonymarschall Mar 19 '12 at 10:46
  • Ok suppose that I've :class => 'test_color' how can I definy my CSS stylesheet? I meen something such as #test_color{a:link { color: #333333; } a:visited { color: #FFFFFF; } a:hover { color: #CCCCCC; } a:active { color: #333333; } } – Marco Mar 19 '12 at 11:02
  • This is a good answer. You don't need a class or curly braces in your link_to in order to accomplish this. Just copy the css from this answer into your CSS file, and the link_to links will color accordingly. – emery Oct 31 '14 at 01:35
2

try this:

= link_to name, url, style: 'color:#FFFFFF;'
Sherlock
  • 74
  • 4
2

I am pretty sure this code will work.

<%= link_to "button_name",{:controller => 'controller_name', :action => 'action_name'},{:style=>"color:#fff;"}%>

Sobin Sunny
  • 1,121
  • 10
  • 14
1

If you have a class called test-color, you can assign the :hover selector to that class by joining the class name and the :hover selector together.

Class hooks begin with a dot(.), IDs begin with a hash(#)

.test-color:link {
  color: #333333;
}
.test-color:visited {
  color: #FFFFFF;
}
.test-color:hover {
  color: #CCCCCC;
}
.test-color:active {
  color: #333333;
}
m4n0
  • 29,823
  • 27
  • 76
  • 89
atw
  • 5,428
  • 10
  • 39
  • 63
0

link_to can be written as

<%= link_to text, path, class: "" %>

Or

<%= path, class: "" do %>
  <div>
    <!-- Insert HTML here -->
  </div>
<% end %>
Cody Elhard
  • 655
  • 1
  • 7
  • 17