Ruby 我的partials不';行不通
我正在尝试将HAML视图拆分为单独的文件。我已经阅读并尝试实现基本版本 因此,我创建了app_helpers.rb文件:Ruby 我的partials不';行不通,ruby,windows,sinatra,haml,Ruby,Windows,Sinatra,Haml,我正在尝试将HAML视图拆分为单独的文件。我已经阅读并尝试实现基本版本 因此,我创建了app_helpers.rb文件: # Usage: partial :foo helpers do def partial(page, options={}) haml page, options.merge!(:layout => false) end end 并在我的应用程序中需要它。rb: require 'sinatra' require_relative './app_hel
# Usage: partial :foo
helpers do
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
end
并在我的应用程序中需要它。rb:
require 'sinatra'
require_relative './app_helpers.rb'
= partial :test
然后我创建了部分视图/test.haml:
<h3> Test message </h3>
但当我刷新页面时,会收到错误消息:
NoMethodError - undefined method `partial' for #<Application:0x514dbe0>:
c:/Dropbox/development/myprojects/test/sinatra-bootstrap/views/index.haml:27:in `evaluate_source'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb:209:in `instance_eval'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb:209:in `evaluate_source'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb:144:in `cached_evaluate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb:127:in `evaluate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/haml.rb:24:in `evaluate'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/tilt-1.3.3/lib/tilt/template.rb:76:in `render'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:625:in `render'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:522:in `haml'
原因是什么?无法复制:
C:\test>cat views\layout.haml
%body= yield
C:\test>cat views\index.haml
#main=partial :test
C:\test>cat views\test.haml
%h3 Hello World
C:\test>cat test_partial.rb
require 'sinatra'
require 'haml'
helpers do
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
end
get '/' do
haml :index
end
c:\test>ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
C:\test>curl http://127.0.0.1:4567/
<body><div id='main'><h3>Hello World</h3></div></body>
西纳特拉游击队在比赛中表现出色。通常会有一个包含您需要创建的文件的文件。然而,他们现在创造了一块宝石
我的建议是将此gem用于partials()。sinatra找不到helper方法。您重新启动了应用程序吗?如果不将其拆分为单独的文件并将
helpers
块直接放在应用程序.rb
文件中,会发生什么情况?我重新启动了应用程序。我把app_helpers.rb放到application.rb。问题依旧,我已经更新了答案。如果你对这种行为有任何想法,请发表评论。这种方法是没有必要的。您只需调用haml:test
。Sinatra(自1.2.0起)将自动检测此调用来自另一个模板,并将:layout
选项设置为nil
。
C:\test>cat views\layout.haml
%body= yield
C:\test>cat views\index.haml
#main=partial :test
C:\test>cat views\test.haml
%h3 Hello World
C:\test>cat test_partial.rb
require 'sinatra'
require 'haml'
helpers do
def partial(page, options={})
haml page, options.merge!(:layout => false)
end
end
get '/' do
haml :index
end
c:\test>ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
C:\test>curl http://127.0.0.1:4567/
<body><div id='main'><h3>Hello World</h3></div></body>