【问题标题】:Rails- nested content_tagRails - 嵌套的 content_tag
【发布时间】:2011-05-11 11:23:54
【问题描述】:

我正在尝试将内容标签嵌套到自定义助手中,以创建如下内容:

<div class="field">
   <label>A Label</label>
   <input class="medium new_value" size="20" type="text" name="value_name" />
</div>

请注意,输入与表单无关,它将通过javascript保存。

这是帮助程序(它会做更多的事情,然后只是显示 html):

module InputHelper
    def editable_input(label,name)
         content_tag :div, :class => "field" do
          content_tag :label,label
          text_field_tag name,'', :class => 'medium new_value'
         end
    end
end

<%= editable_input 'Year Founded', 'companyStartDate' %>

但是,当我调用助手时,标签不显示,只显示输入。如果它注释掉 text_field_tag,则显示标签。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    用迭代构建嵌套的内容标签有点不同,每次都会让我感到……这是一种方法:

          content_tag :div do
            friends.pluck(:firstname).map do |first| 
              concat( content_tag(:div, first, class: 'first') )
            end
          end
    

    【讨论】:

      【解决方案2】:

      我使用变量和 concat 来帮助进行更深的嵌套。

      def billing_address customer
        state_line = content_tag :div do
          concat(
            content_tag(:span, customer.BillAddress_City) + ' ' +
            content_tag(:span, customer.BillAddress_State) + ' ' +
            content_tag(:span, customer.BillAddress_PostalCode)
          )
        end
        content_tag :div do
          concat(
            content_tag(:div, customer.BillAddress_Addr1) +
            content_tag(:div, customer.BillAddress_Addr2) +
            content_tag(:div, customer.BillAddress_Addr3) +
            content_tag(:div, customer.BillAddress_Addr4) +
            content_tag(:div, state_line) +
            content_tag(:div, customer.BillAddress_Country) +
            content_tag(:div, customer.BillAddress_Note)
          )
        end
      end
      

      【讨论】:

        【解决方案3】:

        您也可以使用concat 方法:

        module InputHelper
          def editable_input(label,name)
            content_tag :div, :class => "field" do
              concat(content_tag(:label,label))
              concat(text_field_tag(name,'', :class => 'medium new_value'))
            end
          end
        end
        

        来源:Nesting content_tag in Rails 3

        【讨论】:

        • 只要 concat 行在 1 行,这对我有用。不过,我并没有花很长时间玩它,所以可能有一种方法可以多行完成
        • 考虑到 html_safe 问题,这将是一个更好的方法。在非 htmlsafe 字符串之间使用 + 将使所有内容都非 htmlsafe
        • 如果你在一个表单构建器类中,它应该是@template.concat
        • 我认为这是一种比+ 更清洁的解决方案。更具可读性和 Rubocop 不喜欢 +
        【解决方案4】:

        您需要+ 来快速修复:D

        module InputHelper
          def editable_input(label,name)
            content_tag :div, :class => "field" do
              content_tag(:label,label) + # Note the + in this line
              text_field_tag(name,'', :class => 'medium new_value')
            end
          end
        end
        
        <%= editable_input 'Year Founded', 'companyStartDate' %>
        

        content_tag :div块内,只会显示最后返回的字符串。

        【讨论】:

        • 错字(仅在评论中,但有点混乱)-“不是e此行中的+”
        • 添加后,我得到语法错误:语法错误,意外 tIDENTIFIER,期望 kDO 或 '{' 或 '(' text_field_tag name,'', :class=> 'medium new_value' ^
        • 这感觉很脏......是不是因为它是帮助者构建多个内容标签的反模式?
        • 对我不起作用,必须像其他答案建议的那样使用concat
        猜你喜欢
        • 1970-01-01
        • 2015-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        相关资源
        最近更新 更多