0

I'm using Sinatra + rack-flash3 gem. I installed it, but in spite of that it throws an exception. Here is a layout.haml

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

and _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]

The error is

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

I wonder why is this happening?

4

1 に答える 1

2

sinatra アプリに Rack-flash3 を正しく含めましたか? 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 に答える