新手问题 erb 中能否直接写 sytle?

zix · October 03, 2016 · Last by mimosa replied at October 04, 2016 · 2009 hits

比如

<span style="color:red">
    <%= "红" %>
</span>

我只是想让这个字显示红色 我知道一般样式最好分开,不过像这种简单的再写个 css 感觉有点没必要,如何最简单? 有没有类似

<%= "红",style="color:red" %>

之类的写法

有个 content_tag http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-content_tag

但写出来比 HTML 还长,我觉得 HTML 就好了。

可能一开始你觉得没有什么必要,但是为了代码的清洁度,设一个 class 是很有必要的

# foo.erb
<span class="red"></span>

# foo.css
.red { color: red; }

content_tag 通常为了在 helper 里封装带逻辑的自定义 HTML 标签用的,楼上的这种静态场景确实没必要。

zix closed this topic. 05 Oct 01:12
You need to Sign in before reply, if you don't have an account, please Sign up first.