首页 > 解决方案 > 使用 Ruby on Rails 将链接的字体颜色设置为变量

问题描述

如何将链接的字体颜色设置为内联特定变量的值?我可以将其设置为特定颜色,如下所示。

<%= link_to "Click for more information", info_url(@site), style: 'color: #cc0000;' , class: 'button' %>

将变量插入颜色选择器的语法是什么?我没想到下面的代码可以工作,但这就是我想做的事情的本质。

<%= link_to "Click for more information", info_url(@site), style: 'color: @site.colour' , class: 'button' %>

标签: ruby-on-rails

解决方案


你想要的是字符串插值 http://ruby-for-beginners.rubymonstas.org/bonus/string_interpolation.html

IE:

<%= link_to "Click for more information", info_url(@site), style: "color: #{@site.colour}" , class: 'button' %>

推荐阅读