簡體   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