简体   繁体   中英

ruby on rails: (eval):1: syntax error, unexpected $undefined

I'm getting the following syntax error:

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

This is code execution:

_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 

It fails on the capture() call in the root method:

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

self refers to the instance of MenuBuilder and I am positive that a block is passed as the other parameter. If I throw a puts statement in an if block_given? it will execute, but it wont get passed that content_tag line above.

The issue looks like it's in your use of the capture helper method .

That method accepts a block of view code and assigns it to a variable that can be used elsewhere in your view.

Here's more info: http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-capture

Are you sure you're actually passing a block into the code the executes the capture?

You might consider something like this:

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 

Okay, I just had this problem, and I think I figured out how to fix it. The problem is most likely a bug introduced into ActiveRecord where they're overriding Kernel.capture. The fix, I've found, is to use the helper module's capture instead of the class-level capture. So in your case, where you're calling content_tag :ul, capture(self, &block), options , try calling content_tag :ul, @template.capture(self, &block), options instead so you use the helper module's capture method instead of the shoddy one from AR.

Check out these github issues for some more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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