【问题标题】:What is the method in rails that converts an object to HTML id?rails中将对象转换为HTML id的方法是什么?
【发布时间】:2013-09-04 04:07:23
【问题描述】:

我想知道是否有一种现成的帮助方法可以为对象生成 HTML id,例如 content_tag 使用的方法,即 Resource.first 变为 "resource_1"

当然很容易实现:

def resource_to_html_id resource
  "#{resource.class.to_s.underscore}_#{resource.id}"
end

但它看起来应该已经在 Rails 中定义了。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails methods helper


    【解决方案1】:

    您可以使用dom_id 手动创建一个id 字符串(或dom_class 用于类字符串),或content_tag_for(或更专业的div_for)来创建具有这些已定义的id/类的元素。

    【讨论】:

    • 谢谢! dom_id 是。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2011-01-21
    • 2018-06-03
    相关资源
    最近更新 更多