Rails助手不会呈现所有内容

时间:2018-02-07 17:36:10

标签: ruby-on-rails helper

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

试图干掉我的代码我认为将常用代码转换为辅助方法是个好主意。经过一段时间的努力,我仍然无法让我的助手完全渲染所有生成的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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我可以看到的一个问题是你的助手没有“连接”所有生成的内容:

  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

此外,我建议您使用课程li来干你的header内容标记,因为它们与您的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