[英]Rails: simple_format and class conditions
I'm trying to render some text in Rails with simple_format, but I want to add a class to any lines shorter than 100 characters. 我正在尝试使用simple_format在Rails中渲染一些文本,但是我想向所有少于100个字符的行添加一个类。 Is this at all achievable? 这是完全可以实现的吗?
In my view I've: 我认为:
<%= add_titles(simple_format(truncate(@a1b, :length => 1450))) %>
And in my controller I've this: 在我的控制器中,我这样做:
def add_titles(a)
for a.each_line do |b|
if b.length < 100
print(b, :class => 'strong')
else
print b
end
end
end
Any thoughts? 有什么想法吗? Cheers. 干杯。
For anyone else stuck on this problem I've solved it by putting this logic into the pertinent views: 对于解决此问题的其他人,我已通过将以下逻辑放入相关视图中来解决了这一问题:
<% @article.body.each_line do |m| %>
<% if m.length > 70 %>
<%= content_tag(:p, m) %>
<% else %>
<%= content_tag(:h4, m) %>
<% end %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.