0

我正在使用 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

我想知道为什么会这样?

4

1 回答 1

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

于 2012-10-25T13:18:21.253 回答