【问题标题】:Add a link to a label/hint in formtastic with haml使用haml在formtastic中添加指向标签/提示的链接
【发布时间】:2011-10-21 19:52:05
【问题描述】:

我正在尝试使用haml在formtastic中添加一个链接到一个复选框。看起来 formtastic 在解析标签或提示时从文本中删除了所有 ruby​​/haml/html 语法。

也就是说,这不起作用:

= f.input :terms, label: '=link_to \'Here\', path_to_link', hint: '=link_to \'Other Place\', path_to_other_link', as: :boolean

除了在这个输入之外写另一个 div 之外,还有什么可以做的吗?还是我对这一切都错了?我是一个 Rails/haml/formtastic 菜鸟。

【问题讨论】:

    标签: ruby-on-rails haml formtastic


    【解决方案1】:

    如果你使用 ' 作为字符串引用,你基本上是在告诉 ruby​​ 不要解析那个内容。

    试试这样的:

    = f.input :terms, label: link_to('Here', path_to_link), hint: link_to('Other place', path_to_other_link), as: :boolean
    

    【讨论】:

      【解决方案2】:
      Formtastic::FormBuilder.escape_html_entities_in_hints_and_labels = false
      

      在您的格式初始化程序中

      【讨论】:

      • 这是使用包含静态 HTML 内容的 I18n 本地化文件的最佳答案。但是,它不能解决 @tessr 在 Formtastic 标签或提示中嵌入 HAML 的问题。
      【解决方案3】:

      如果您将传递给label:hint: 的字符串标记为HTML 安全,那么Haml 不会对其进行转义。

      = f.input :terms,
        label: "Please accept our ".html_safe + link_to('Terms and Conditions', path_to_link)
      

      【讨论】:

        【解决方案4】:

        尝试了 19 种方法,其中超链接被编码或 html_safe 替换 url 中的连字符???

        这对我有用

        <%= f.label :cookies do
          "Do you agree to our #{link_to('Cookies Policy', 'http://www.your-­url.co.uk/privacypolicy')}".html_safe
        end %>
        

        " 和 ' 的具体用法似乎很重要。

        【讨论】:

          猜你喜欢
          • 2013-01-28
          • 1970-01-01
          • 2020-04-24
          • 2019-01-07
          • 2011-06-07
          • 1970-01-01
          • 2017-11-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多