【问题标题】:Styling colors in Rails 4 inside link_to, but inline styling not displaying correctly在 link_to 内的 Rails 4 中设置颜色样式,但内联样式显示不正确
【发布时间】:2014-06-27 18:55:47
【问题描述】:

<%= link_to "Sign In", new_user_session_path, :method => :get, style: "color:white" %>
<h3>Read More - <%= link_to 'Article', action: :articles, style: "color: purple" %></h3>
<%= link_to 'Click to Read More About What People Are Saying About Us', action: :articles %>

所以我在一个 html.erb 文件的正文中有上述三行代码。

如我所愿,第一个“登录”显示为白色。

第二个“文章”链接显示为黑色!我想要紫色。

第三个,显示为灰色。但我希望它是白色的。

我的问题到底是什么?我正在尝试同时学习 rails 和 css。

以下是我的总体脚手架.css.scss 文件:

body {
  background-color: #fff;
  color: #333;
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

p, ol, ul, td {
  font-family: verdana, arial, helvetica, sans-serif;
  font-size: 13px;
  line-height: 18px;
}

pre {
  background-color: #eee;
  padding: 10px;
  font-size: 11px;
}

a {
  color: #000;
  &:visited {
    color: #666;
  }
  &:hover {
    color: #fff;
    background-color: #000;
  }
}

div {
  &.field, &.actions {
    margin-bottom: 10px;
  }
}

#notice {
  color: green;
}

.field_with_errors {
  padding: 2px;
  background-color: red;
  display: table;
}

#error_explanation {
  width: 450px;
  border: 2px solid red;
  padding: 7px;
  padding-bottom: 0;
  margin-bottom: 20px;
  background-color: black;
  h2 {
    text-align: left;
    font-weight: bold;
    padding: 5px 5px 5px 15px;
    font-size: 12px;
    margin: -7px;
    margin-bottom: 0px;
    background-color: #c00;
    color: #fff;
  }
  ul li {
    font-size: 12px;
    list-style: square;
  }
}

编辑:添加渲染 HTML:

<a data-method="get" href="/users/sign_in" style="color:white">Sign In</a>


<h3>Read More - <a href="/articles?style=color%3A+purple">Articles</a></h3>
<a href="/articles">Click to Read More About What People Are Saying About Us</a>

【问题讨论】:

  • 生成的标签是否正确呈现?
  • 是的,所有标签都链接到正确的视图
  • 你能粘贴呈现的 HTML 吗?

标签: html ruby css ruby-on-rails-4 sass


【解决方案1】:

第二个链接:你需要把html_optionsurl_options分开:

 <h3>Read More - <%= link_to 'Article', {action: :articles}, {style: "color: purple"} %></h3>

否则 rails 会创建一个链接,认为style: ... 是一个额外的 GET 参数,可以通过 url 传递。

与最后一个一样,很难说,您的 css 指令很可能在某个地方被另一个 css 文件覆盖。在您的浏览器上安装 firebug(如果使用 FF/Chrome)或使用开发者工具 (IE) 来查找链接最终颜色的设置位置。

【讨论】:

    【解决方案2】:

    我很确定这段代码会起作用。
    &lt;%= link_to "button_name",{:controller =&gt; 'controller_name', :action =&gt; 'action_name'},{:style=&gt;"color:#fff;"}%&gt;

    【讨论】:

      猜你喜欢
      • 2012-04-03
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 2018-03-02
      • 2022-11-23
      相关资源
      最近更新 更多