Ruby / Sinatra / HAML flash message problem
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 som开发者_如何学Pythoneone 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?
I fixed it by using this instead:
http://github.com/nakajima/rack-flash
精彩评论