Ruby / Sinatra / HAML flash issue

I have the following small Sinatra application (I removed the excess unnecessary 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 main layout template file that the application uses:

#message

    - if @flash[:error]
        %p.error 
            = @flash[:error]

    - if @flash[:notice]
        %p.notice
            = @flash[:notice]

So, theoretically, when someone sends a message, an auxiliary flash () is called and a session variable is set, then the request is redirected when it comes in and it must set the session variable to an instance of a variable accessible by template before starting the filter.

However, for life, I can’t understand why it does not print the message in the template.

Any ideas?

+3
source share
1 answer

I fixed this using this instead:

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

+5

Source: https://habr.com/ru/post/1729798/


All Articles