Ruby / Sinatra / HAML 闪现消息问题
我有以下小型 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( ) 调用帮助器并设置会话变量,然后当 before 过滤器启动时请求被重定向,并且它应该将会话变量设置为模板可访问的实例变量。
然而,我一生都无法弄清楚为什么它没有在模板中打印消息。
有什么想法吗?
I have the following small Sinatra application (I've removed the extra unneeded code):
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
And then I have the following in the HAML base layout template file that the application uses:
#message
- if @flash[:error]
%p.error
= @flash[:error]
- if @flash[:notice]
%p.notice
= @flash[:notice]
So, in theory, when someone posts a message, the flash() helper is called and a session variable is set, then the request is redirect, when the before filter kicks in and it should set the session variable to an instance variable accessible by the template.
However, for the life of me I cannot figure out why it's not printing the message in the template.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用这个来修复它:
http://github.com/nakajima/rack-flash
I fixed it by using this instead:
http://github.com/nakajima/rack-flash