2

I'm trying to change the following Rails link into primary color (it's currently white):

<%= link_to 'Edit' %>
R.P.
  • 178
  • 1
  • 12
Rony M.
  • 405
  • 1
  • 7
  • 16
  • 1
    Possible duplicate of [Rails link\_to with inline styling](https://stackoverflow.com/questions/9768518/rails-link-to-with-inline-styling) – Berkhan Berkdemir Apr 28 '18 at 23:05

2 Answers2

7

You can add a class to your link after providing the path.

<%= link_to 'Edit', edit_path, class: "a-primary" %>

And add styles for that class.

.a-primary{
  /* styles for link */
}
Kumar
  • 3,116
  • 2
  • 16
  • 24
2

If your application has a common style, there's no need to define a CSS class. Just write the CSS for <a> tags in general:

a:link, a:visited {
background-color: #f44336;
color: white;
padding: 14px 25px;
text-align: center; 
text-decoration: none;
display: inline-block;
}

a:hover, a:active {
  background-color: red;
}

Ruby code

<%= link_to 'Edit' , '#'%>

Rails renders the link_to script as a normal hyperlink in HTML.

<a href="#" > Edit</a>

then the browser will apply the style to all hyperlinks in the app.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Anand Jose
  • 638
  • 9
  • 26