我有以下小型 Sinatra 应用程序(我已经删除了多余的不需要的代码):
helpers do
def flash(args={})
session[:flash] = args
end
def flash_now(args={})
@flash = args
end
end
before do
@flash = session[:flash] || {}
session[:flash] = nil
end
post '/post' do
client = Twitter::Client.new(:login => 'xxxxxxx', :password => 'xxxxxxx')
username = params[:username]
type = params[:type]
tags = params[:tags]
budget = params[:budget]
if username != '' && type != '' && tags != '' && budget != ''
message = username + ' is looking for a ' + type + ' with ' + tags + ' skills. Budget = ' + budget + ' #freelance #job'
status = client.status(:post, message)
flash(:notice => 'Gig posting sent successfully!')
else
flash(:error => 'Gig posting unsuccessful - please check the marked fields!')
end
redirect '/'
end
然后我在应用程序使用的 HAML 基本布局模板文件中有以下内容:
#message
- if @flash[:error]
%p.error
= @flash[:error]
- if @flash[:notice]
%p.notice
= @flash[:notice]
因此,理论上,当有人发布消息时,会调用 flash() 帮助程序并设置会话变量,然后重定向请求,当之前的过滤器启动时,它应该将会话变量设置为可访问的实例变量模板。
但是,对于我的生活,我无法弄清楚为什么它没有在模板中打印消息。
有任何想法吗?