2012-10-25 29 views
0

我使用Sinatra + rack-flash3寶石。我安裝了它,但儘管如此,它拋出了一個異常。這裏是一個layout.hamlSinatra對寶石視而不見?

!!!5 
%html 
    %body 
    .my_div 
     != haml :"shared/_flash" 
     != yield 

_flash.haml

.my-div2 
    .flash-notice 
    - if flash[:notice] 
     .alert.alert-success 
     = flash[:notice] 
    - if flash[:error] 
     .alert.alert-error 
     = flash[:error] 
    - if flash[:info] 
     .alert.alert-info 
     = flash[:info] 

的誤差

undefined local variable or method `flash' for #<App:0x00000004226c90> 
file: _flash.haml location: evaluate_source line: 3 

我不知道爲什麼會出現這種情況?

回答

2

您是否正確地將rack-flash3放入您的sinatra應用程序中?你必須use Rack::Flash在您的應用程序的頂部,就像這樣:

require 'sinatra/base' 
require 'rack-flash' 

class MyApp < Sinatra::Base 
    enable :sessions 
    use Rack::Flash 

    post '/set-flash' do 
    # Set a flash entry 
    flash[:notice] = "Thanks for signing up!" 

    # Get a flash entry 
    flash[:notice] # => "Thanks for signing up!" 

    # Set a flash entry for only the current request 
    flash.now[:notice] = "Thanks for signing up!" 
    end 
end 

來源:http://rubydoc.info/gems/rack-flash3/1.0.1/frames

+0

我有同樣的問題,但不能得到這個工作。你剛剛說了什麼,但無濟於事。任何幫助,高度讚賞。 – AMBasra