M. Wood
M. Wood

Reputation: 63

Variable formatting in Link_to

I am looking to pull a hex value from a field into the link_to line for rails. I have found how to format a standard link in line with the following

 <td><%= link_to 'Show', security, {:style=>'color:#FF6A00;', :class => 'css_class'} %></td>

I am looking to make the color a change value based on the specific record. I know that this goes in the face of typical css formatting. I don't want to do the color evaluation in the css file as there will be many, many colors that will be used. I can make standard text change color with the following code

<td style="color:#<%= security.subcategory.color %>"><%= security.subcategory.color %></td>

For some reason, this approach does not work within the link_to line. I'm sure that I am missing so sort of formatting with the code. I'm sure that I am missing something with formatting a link inline.

Thanks for the help

Upvotes: 0

Views: 115

Answers (2)

craig.kaminsky
craig.kaminsky

Reputation: 5598

Assuming you're not trying to change the color of the link dynamically after the DOM has loaded, your code is quite close and you should be able to 'combine' your two code snippets above as:

<td><%= link_to 'Show', security, { style:"color:##{security.subcategory.color};", class: 'css_class'} %></td>
# note that the double pound sign (##) is intentional. 
# The first is for your actual CSS declaration 
# The second is part of Ruby's string interpolation so that security.subcategory.color is rendered in the style option

This code snippet should color the link and not the table cell.

Upvotes: 0

Axel Tetzlaff
Axel Tetzlaff

Reputation: 1364

The color of the TD is not inherited to the link by default.

Check: CSS a:link keep original color

Upvotes: 1

Related Questions