简体   繁体   English

ruby on rails:(eval):1:语法错误,意外的$ undefined

[英]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.html.haml

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

builders_helper.rb builders_helper.rb

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

menu_builder.rb 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: 它在root方法中的capture()调用上失败:

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. self指的是MenuBuilder的实例,我很肯定将一个块作为另一个参数传递。 If I throw a puts statement in an if block_given? 如果我在if block_given中抛出puts语句? it will execute, but it wont get passed that content_tag line above. 它会执行,但不会通过上面的content_tag行。

The issue looks like it's in your use of the capture helper method . 问题似乎出在您使用capture助手方法时

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 这是更多信息: 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. 问题很可能是ActiveRecord中引入的一个错误,该错误覆盖了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. 因此,在您的情况下,您在调用content_tag :ul, capture(self, &block), options ,请尝试调用content_tag :ul, @template.capture(self, &block), options以便改用helper模块的capture方法来自AR的劣质产品之一。

Check out these github issues for some more info. 查看这些 github 问题以获取更多信息。

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

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