Ruby / Sinatra / HAML 闪现消息问题

发布于 2024-08-18 14:37:52 字数 1336 浏览 3 评论 0原文

我有以下小型 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2024-08-25 14:37:52

我用这个来修复它:

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

I fixed it by using this instead:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文