Rails 助手不呈现所有内容

Rails helper doesn't render all content

我有一些代码在我的许多页面上使用,并且在进行更改时返回并更新所有内容开始变得有点费力。

尝试整理我的代码我认为将常用代码转换为辅助方法是个好主意。在这方面工作了一段时间后,我仍然无法让我的助手完全渲染所有生成的 html,而且我无法弄清楚我做错了什么。

这是我的帮手:

module TablesHelper
  def table(collection, *args)
    content_tag :ul, class: "table help" do
      #first off we need a header row
      content_tag :li, class: "header" do
        args.collect do |option|
          content_tag :span, option.to_s.titleize
        end.join(' ').html_safe
      end

      collection.collect do |object|
        content_tag :li do
          args.collect do |param|
            content_tag :span, object.send(param)
          end.join(' ').html_safe
        end
      end.join(' ').html_safe
    end
  end

  def link_table(collection, *args)
    content_tag :ul, class: "table help" do
      #first off we need a header row
      content_tag :li, class: "header" do
        args.collect do |arg|
          content_tag :span, arg.to_s.titleize
        end.join(' ').html_safe
      end

      collection.collect do |object|
        content_tag :li do
          args.collect do |arg|
            #the first item on this row needs to be a link
            if arg.equal? args.first
              link_to object.send(arg), object
            else
              content_tag :span, object.send(arg)
            end
          end.join(' ').html_safe
        end
      end.join(' ').html_safe
    end
  end

  def button_table(collection, *args)
    content_tag :ul, class: "table help" do
      #first off we need a header row
      concat content_tag :li, class: "header" do
        args.collect do |option|
          concat content_tag :span, option.to_s.titleize
        end.join(' ').html_safe
      end

      collection.collect do |object|
        concat content_tag :li do
          args.collect do |param|
            concat content_tag :span, object.send(param)
          end.join(' ').html_safe
          #lets throw on the small buttons
          concat content_tag :div, class: "options" do
            concat link_to content_tag(:i, nil, :class => "fa fa-eye"), object, class: "small primary button"
            concat link_to content_tag(:i, nil, :class => "fa fa-pencil"), [:edit, object], class: "small primary button"
          end
        end
      end.join(' ').html_safe
    end
  end
end

我做错了什么?

我看到的一个问题是您的助手没有 "concatenate" 所有生成的内容:

  def link_table(collection, *args)
    content_tag :ul, class: "table help" do
      content_tag :li, class: "header" do
        args.collect do |arg|
          # ...
        end.join(' ').html_safe
      end
      # RIGHT HERE, the above is not "concatenated" to the content below
      collection.collect do |object|
        content_tag :li do
          args.collect do |arg|
            # ...
          end.join(' ').html_safe
        end
      end.join(' ').html_safe
    end
  end

通常,对于这种类型的助手(生成 HTML),我会执行以下模式:

def link_table(collection, *args)
  content = ''.html_safe

  content += content_tag(:li, class: 'header') do
    # ...
  end

  content += collection.collect do |object|
    content_tag :li do
      # ...
    end
  end.join(' ')

  content_tag(:ul, class: 'table help') do
    content
  end
end

此外,我建议您使用 class header 干燥您的 li 内容标签,因为它们对您的 3 个助手来说是相同的。类似于:

def li_header(collection)
  content_tag :li, class: "header" do
    collection.collect do |item|
      content_tag :span, item.to_s.titleize
    end.join(' ').html_safe
  end
end

并像这样使用它:

def link_table(collection, *args)
  content = ''.html_safe

  content += li_header(collection)

  # ...

  content_tag(:ul, class: 'table help') do
    content
  end
end