我正在使用Sinatra + rack-flash3 gem。我安装了它,但尽管如此它抛出异常。这是layout.haml
!!!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
我想知道为什么会这样?
答案 0 :(得分: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