2012-11-21 66 views
2

我试图使用rails帮助器方法生成格式化的html字符串,该方法位于application_helper.rb从Rails方法返回html字符串

def comment_posted_tag(actor_id,date) 
    ... irrelevant code removed 
    str = "<b>" + actor_name + "</b>" + " on " + date.strftime("%d %b %Y at %H:%M") 
    return str.html_safe 
end 

我打电话像这样的观点:

<%= comment_posted_tag(dialog_comment.actor_id,dialog_comment.updated_at) %> 

出于某种原因,标签不来通过。理想情况下,我想将一个css类附加到actor_name字符串中。我试过了,没有html_safe,但都没有工作。

回答

3

始终使用content_tag对于这样的工作:

def comment_posted_tag(actor_name, date) 
    content_tag(:span, :class => 'yours') do 
    content_tag(:b, actor_name) + ' on ' + date.strftime("%d %b %Y at %H:%M") 
    end 
end 
+0

完美。非常感谢。 – ardochhigh