Подтвердить что ты не робот

Вложенный content_tag throws undefined метод `output_buffer =` в простой помощник

Я пытаюсь создать простой помощник вида, но как только я попытаюсь вложить пару тегов контента, он бросит

NoMethodError: undefined method `output_buffer=' for
def table_for(list, &proc)
  t = Table.new
  proc.call(t)
  t.render_column(list) 
end

class Table
  include ActionView::Helpers::TagHelper

  attr_accessor :columns, :block

  def initialize
    @columns = Array.new
  end

  def col(name)
    @columns << name
  end

  def render_column(list)
    content_tag :table do
      list.each do |c|
        content_tag :td, c
      end
    end
  end
end

Любые намеки на то, что неправильно? Я также видел, что XmlBuilder - это то, что лучше для моей цели?

4b9b3361

Ответ 1

С помощью Вложенный content_tag throws undefined метод` output_buffer = `в простой помощник Я закончил со следующим решением, основанным на API для Formtastic.

<%= table_for(@users) do |t| %>
   <% t.col :name %>
   <% t.col :email %>
   <% t.col :test, :value => lambda { |u| u.email }, :th => 'Custom column name' %>
   <% t.col :static, :value => 'static value' %>
<% end %>

Используя output_buffer напрямую и, возможно, изобретая колесо, код выглядит как

module ApplicationHelper
  def table_for(list, &block)
    table = Table.new(self)
    block.call(table)
    table.show(list)
  end

  class Column
    include ActiveSupport::Inflector

    attr_accessor :name, :options

    def initialize(name, options = {})
      @name    = name
      @options = options
    end

    def td_value(item)
      value = options[:td]
      if (value)
        if (value.respond_to?('call'))
          value.call(item)
        else
          value
        end
      else
        item[name]
      end
    end

    def th_value
      options[:th] ||= humanize(name)
    end
  end

  class Table
    include ActionView::Helpers::TagHelper

    attr_accessor :template, :columns

    def initialize(temp)
      @columns  = Array.new
      @template = temp
    end

    def col(name, options = {})
      columns << Column.new(name, options)
    end


    def show(list)
      template.content_tag(:table) do
        template.output_buffer << template.content_tag(:tr) do
          columns.collect do |c|
            template.output_buffer << content_tag(:th, c.th_value)
          end
        end
        list.collect do |item|
          template.output_buffer << template.content_tag(:tr) do
            columns.collect do |c|
              template.output_buffer << template.content_tag(:td, c.td_value(item))
            end
          end
        end
      end
    end

  end
end

Ответ 2

ActionView:: Base встроил в него модуль Context, который предоставляет методы output_buffer() и output_buffer =().

Итак, вы можете решить свою проблему, выполнив свой класс:

include ActionView::Context

Или еще проще:

attr_accessor :output_buffer

Ответ 3

Я думаю, что в 3.0 были внесены некоторые изменения, но в предыдущих версиях трюк состоял в том, чтобы передать self:

def table_for(list, &proc)
  Table.new(self)
  # ...

def initialize(binding)
  @binding = binding
  #...

def render_column
  @binding.content_tag :table do
    # ...
  end
end

Я не уверен, что это все еще, как это делается в rails 3.

Еще одна вещь, которую нужно исправить в ordere для работы кода - это сохранить результат внутреннего содержимого content_tag где-то, как и в each, содержимое будет сгенерировано и затем отброшено. Одно из возможных решений:

def render_column(list)
  @binding.content_tag :table do
    list.inject "" do |out, c|
      out << @binding.content_tag(:td, c)
    end.html_safe
  end
end