【问题标题】:f.label not working with block in hamlf.label 不适用于 haml 中的块
【发布时间】:2013-01-22 11:26:08
【问题描述】:

我有以下haml代码

form_for @user_notification_settings_form do |f|
  = f.label :follow do
    div dummy

但它会生成

<label for="user_notification_settings_form_follow">
  foruser_notification_settings_form_follow
</label>

我期望它在哪里生成

<label for="user_notification_settings_form_follow">
  <div>dummy</div>
</label>

调试到我发现的rails:

label_tagform_tag_helper&amp;block 但当它在 tag_helper 中调用 content_tag 时,&amp;block 现在变为 nil

  def label_tag(name = nil, content_or_options = nil, options = nil, &block)
    options = content_or_options if block_given? && content_or_options.is_a?(Hash)
    options ||= {}
    options.stringify_keys!
    options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
    content_tag :label, content_or_options || name.to_s.humanize, options, &block
  end
  def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
    if block_given?
      options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
      content_tag_string(name, capture(&block), options, escape)
    else
      content_tag_string(name, content_or_options_with_block, options, escape)
    end
  end

【问题讨论】:

    标签: ruby ruby-on-rails-3 haml


    【解决方案1】:

    您只需要制作%div 元素,然后将标签的文本嵌套在其中。

    form_for @user_notification_settings_form do |f|
      = f.label :follow do
        %div 
          dummy
    

    【讨论】:

    • %div 的任何原因?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    相关资源
    最近更新 更多