繁体   English   中英

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

我在这里做错了什么?

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

  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

另外,我建议您使用类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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM