繁体   English   中英

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

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

我收到以下语法错误:

(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行。

问题似乎出在您使用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 

好的,我只是遇到了这个问题,我想我已经找到了解决方法。 问题很可能是ActiveRecord中引入的一个错误,该错误覆盖了Kernel.capture。 我发现,解决方法是使用帮助程序模块的捕获而不是类级捕获。 因此,在您的情况下,您在调用content_tag :ul, capture(self, &block), options ,请尝试调用content_tag :ul, @template.capture(self, &block), options以便改用helper模块的capture方法来自AR的劣质产品之一。

查看这些 github 问题以获取更多信息。

暂无
暂无

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

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