我正在尝试创建一个ruby类来管理我的应用程序中发生的大部分事情,并且我打算通过嵌入式Ruby代码来管理它的参数。我认为它是这样的,但它显然不起作用:
require 'sinatra'
require './models/questionaire_manager'
set :bind, '0.0.0.0'
set :port, ENV['PORT']
enable :sessions
set :session_secret, 'SecretString#!$%'
get '/' do
@questionaire=Questionaire_Manager.new 0
erb :index
end
post '/' do
session[:number]=params[:number]
redirect '/quiz'
end
get '/quiz' do
@questionaire.number=session[:number]
@questionaire.genQuestionaire
erb :quiz
end
post '/quiz' do
redirect'/results'
end
get '/results' do
@number=session[:number]
erb :results
end
我想我也应该说我无法掌握会话和会话参数,而且由于Sinatra的页面已经关闭了将近一周,我真的无法查看它。
答案 0 :(得分:0)
尝试这样的事情吗?
require 'sinatra'
require './models/questionaire_manager'
set :bind, '0.0.0.0'
set :port, ENV['PORT']
enable :sessions
set :session_secret, 'SecretString#!$%'
helpers do
def quiz_manager
@questionaire = session[:quiz_manager] ||= Questionaire_Manager.new 0
end
end
get '/' do
# Uncomment the line below if you intend to create a new quiz each time
# session[:quiz_manager] = nil
quiz_manager # Initializes the session variable
erb :index
end
post '/' do
quiz_manager.number = params[:number]
redirect '/quiz'
end
get '/quiz' do
quiz_manager.genQuestionaire
erb :quiz
end
post '/quiz' do
redirect '/results'
end
get '/results' do
@number = quiz_manager.number
erb :results
end
修改强>
为了澄清这是做什么的 - 我创建了一个名为quiz_manager的辅助方法,如果它还没有被设置,则初始化session[:quiz_manager]
- 这将在路由之间保持不变。我还设置了类变量@questionnaire
,以便您可以在视图中访问它。