16

私は次のことをしようとしています:

module ApplicationHelper

   class PModuleHelper
     include ActionView::Helpers::TagHelper
     def heading(head = "", &block)
       content = block_given? ? capture(&block) : head.to_s
       content_tag :h3, content, :class => :module_header
     end
   end

    def getmh
        PModuleHelper.new
    end

end

メソッドheadingまたはブロックに文字列 (または記号) を指定します。

ビューで:

<% mh = getmh %>
<%= mh.heading :bla %> // WORKS
<%= mh.heading do %>   // FAILS
    test 123
<% end %>

(getmhはこの例のためPModuleHelperのものであり、アプリケーションの他のプロセスによって返されるため、これについてコメントしたりheading、クラスメソッドではなく通常のヘルパーメソッドを作成することを提案したりする必要はありません)

残念ながら、私は常に次のエラーが発生します。

wrong number of arguments (0 for 1)

capture(&block)コールの回線番号付き。

capture独自のヘルパー クラス内で使用する方法は?

4

1 に答える 1

13

私はこのようなことをします:

module Applicationhelper
  class PModuleHelper

    attr_accessor :parent

    def initialize(parent)
      self.parent = parent
    end

    delegate :capture, :content_tag, :to => :parent

    def heading(head = "", &block)
      content = block_given? ? capture(&block) : head.to_s
      content_tag :h3, content, :class => :module_header
    end
  end

  def getmh
    PModuleHelper.new(self)
  end
end

undefined method 'output_buffer='あなたが言及しているエラーの代わりに、このエラーが発生したため、これが機能することを保証できません。私はあなたのものを再現できませんでした。

于 2011-08-28T17:29:34.353 に答える