【问题标题】:Returning html string from Rails method从 Rails 方法返回 html 字符串
【发布时间】:2012-11-21 13:13:25
【问题描述】:

我正在尝试使用位于 application_helper.rb 中的 rails helper 方法构建格式化的 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 的情况下都试过了,但都不管用。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    在这种工作中总是使用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
    

    【讨论】:

      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多