【问题标题】:How to get HTML tag included in the output using a helper method如何使用辅助方法获取包含在输出中的 HTML 标记
【发布时间】:2013-09-07 06:39:33
【问题描述】:

我想在助手中使用html标签

def get_hotel_theme_names(hotel)
    themes = hotel.themes.map(&:theme_names).to_a
    themes = themes.blank? ? nil :  themes.join(", ")
    themes
end

我有get_theme_name helper 方法返回themes of hotel,但我想在每个主题上使用链接

点赞:get_hotel_theme_name 返回funny, entertainment, fun, music,

所以我想将主题命名为:funny (X), entertainment (X), fun (X), music (X),

其中(X)linkdelete theme,那么如何在上面添加链接

【问题讨论】:

  • 通过这样的助手似乎不是获得你想要的东西的理想方式。为什么不直接遍历视图中的hotel.themes

标签: html ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1


【解决方案1】:

只需将您想要的任何标签连接到输出中。

def get_hotel_theme_names(hotel)
    output = []
    hotel.themes.each do |theme|
      output << (theme.theme_names + link_to('(X)', theme, :method=>:delete, :confirm=>'sure?'))          
      #or add other tags you want
      #output << '<div>test</div>'
    end

    output.join(',').html_safe
end

【讨论】:

    猜你喜欢
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多