Ruby/Sinatra/HAML flash消息问题

Ada*_*lor 3 ruby haml sinatra

我有以下小型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
Run Code Online (Sandbox Code Playgroud)

然后我在应用程序使用的HAML基本布局模板文件中有以下内容:

#message

    - if @flash[:error]
        %p.error 
            = @flash[:error]

    - if @flash[:notice]
        %p.notice
            = @flash[:notice]
Run Code Online (Sandbox Code Playgroud)

因此,理论上,当有人发布消息时,调用flash()帮助程序并设置会话变量,然后请求重定向,当前过滤器启动时,它应该将会话变量设置为可访问的实例变量模板.

但是,对于我的生活,我无法弄清楚为什么它不在模板中打印消息.

有任何想法吗?

Ada*_*lor 5

我通过使用它来修复它:

http://github.com/nakajima/rack-flash