2013-06-22 66 views
1

可能是相当基本的东西,但我希望能够在模块化Sinatra应用程序中使用一些自定义帮助程序方法。我在./helpers/aws_helper.rb以下NoMethodError Sinatra模块化应用程序

helpers do 
def aws_asset(path) 
    File.join settings.asset_host, path 
end 
end 

,然后在我看来,我希望能够用这种方法,像这样

<%= image_tag(aws_asset('/assets/images/wd.png')) %> 

,但我得到上面的面积,从而在我的app.rb文件我

require './helpers/aws_helper' 


class Profile < Sinatra::Base 

get '/' do 
    erb :index 
end 

end 

所以是我的问题,我要求它在我的配置文件类之外。这是没有意义的,因为我需要我的配置文件的ENV变量相同的方式,他们正在阅读,但他们又不是方法,所以我想这是有道理的。

我想也许我正在努力让自己的头脑模糊一下模块化应用程序,而不是使用经典风格的sinatra应用程序。

任何指针赞赏

错误消息

NoMethodError at/undefined method `aws_asset' for #<Profile:0x007f1e6c4905c0> file: index.erb location: block in singletonclass line: 8 
+0

你真的错过你'require'行的结尾撇号?什么是你收到的实际的完整错误信息? – Casper

+0

不,这是一个错字,已经修改,我也添加了我得到的错误 – Richlewis

回答

2

当您在顶层使用helpers do ...这样你添加的方法作为助手Sinatra::ApplicationProfile类。如果您正在使用Sinatra模块化风格,请确保您只使用require 'sinatra/base'而不是require sinatra,这会阻止您混合使用这两种风格。

在这种情况下,您应该为您的帮手创建一个模块,而不是使用helpers do ...,然后在Profile类中将该模块与helpers method一起添加。

helpers/aws_helper.rb

module MyHelpers # choose a better name of course 

    def aws_asset(path) 
    File.join settings.asset_host, path 
    end 
end 

app.rb

class Profile < Sinatra::Base 

    helpers MyHelpers # include your helpers here 

    get '/' do 
    erb :index 
    end 
end 
相关问题