Rails link_to with inline styling

风流意气都作罢 提交于 2019-11-27 15:34:37

问题


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


回答1:


How about

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

...or...

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



回答2:


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'



回答3:


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.




回答4:


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' %>



回答5:


try this:

= link_to name, url, style: 'color:#FFFFFF;'



回答6:


I am pretty sure this code will work.

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




回答7:


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;
}



回答8:


link_to can be written as

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

Or

<%= path, class: "" do %>
  <div>
    <!-- Insert HTML here -->
  </div>
<% end %>


来源:https://stackoverflow.com/questions/9768518/rails-link-to-with-inline-styling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!