开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜