私は次の小さなSinatraアプリケーションを持っています(余分な不要なコードを削除しました):
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
そして、アプリケーションが使用するHAMLベースレイアウトテンプレートファイルに次のものがあります。
#message
- if @flash[:error]
%p.error
= @flash[:error]
- if @flash[:notice]
%p.notice
= @flash[:notice]
したがって、理論的には、誰かがメッセージを投稿すると、flash()ヘルパーが呼び出され、セッション変数が設定されます。その後、beforeフィルターが開始され、セッション変数をによってアクセス可能なインスタンス変数に設定する必要があるときに、要求はリダイレクトされます。テンプレート。
しかし、私の人生では、なぜそれがテンプレートにメッセージを印刷しないのか理解できません。
何か案は?