2

次の構文エラーが発生します。

(eval):1: syntax error, unexpected $undefined
$#<MenuBuilder:0x007fccee84e7f8> = #<MENUBUILDER:0X007FCCEE84E7F8>
 ^

これはコード実行です:

_main_menu.html.haml

#main-menu 
  = menu do |m| 
    = m.submenu "Products" do 
      = m.item "Products", Product 

builders_helper.rb

module BuildersHelper 
  def menu(options = {}, &block) 
    MenuBuilder.new(self).root(options, &block) 
  end 
end 

menu_builder.rb

class MenuBuilder 
  attr_accessor :template 
  def initialize(template) 
    @template = template 
  end 
  def root(options, &block) 
    options[:class] = "jd_menu jd_menu_slate ui-corner-all" 
    content_tag :ul, capture(self, &block), options 
  end 
  def item(title, url, options = {}) 
    content_tag :li, options do 
      url = ajax_route(url) unless String === url 
      url = dash_path + url if url.starts_with?("#") 
      link_to title, url 
    end 
  end 
  def submenu(title, options = {}, &block) 
    content_tag :li do 
      content_tag(:h6, title.t) + 
      content_tag(:ul, capture(self, &block), :class => "ui-corner- 
all") 
    end 
  end 
end 

root メソッドでの capture() 呼び出しで失敗します。

content_tag :ul, capture(self, &block), options

self は MenuBuilder のインスタンスを参照しており、ブロックが他のパラメーターとして渡されることは確かです。if block_given で puts ステートメントをスローすると? 実行されますが、上記の content_tag 行は渡されません。

4

2 に答える 2

0

この問題はcaptureヘルパー メソッドの使用にあるようです。

このメソッドは、ビュー コードのブロックを受け取り、それをビューの他の場所で使用できる変数に割り当てます。

詳細はこちら: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture

キャプチャを実行するコードに実際にブロックを渡していますか?

次のように考えることができます。

content_tag :li do 
  if block_given?
    content_tag(:h6, title.t) + 
    content_tag(:ul, capture(self, &block), :class => "ui-corner-all") 
  else
    content_tag(:h6, title.t) # whatever is appropriate if there's no block passed
  end
end 
于 2012-05-09T21:51:29.940 に答える