嗨,如果我有一个带有以下代码的'main'sinatra文件,
require 'sinatra'
require "sinatra/reloader"
class MyApp < Sinatra::Base
configure do
require "./rest/auth.rb"
register Sinatra::Reloader
also_reload '/rest/auth'
end
get '/' do
erb :home
end
end
我想将我的身份验证逻辑放在/rest/auth.rb中,如何在开发模式下重新加载/rest/auth.rb?我必须在每个控制器文件中放置配置块并要求sinatra / reloader吗?我希望我的控制器继承我的主类的设置。 auth.rb中的代码如下:
class MyApp < Sinatra::Base
set(:auth) do |*roles| # <- notice the splat here
condition do
unless logged_in?
session[:success_url] = request.path_info
redirect '/'
end
end
end
def logged_in?
current_user
end
def current_user
if session[:user_id]
u = User.find(:id=>"#{session[:user_id]}")
else
false
end
end
end
我需要重新启动服务器才能进行更改但是我可以在auth.rb的配置块中抛出重新加载代码,尽管我不愿意。有什么想法吗?
答案 0 :(得分:9)
尝试像这样重写
require 'sinatra/base'
require "sinatra/reloader"
class MyApp < Sinatra::Base
configure :development do
register Sinatra::Reloader
also_reload './rest/auth'
end
require "./rest/auth.rb"
get '/' do
erb :home
end
end