【Ruby on Rails】Action Dispatch Flash
flash はアクション間で一時的なプリミティブ型(String、Array、Hash)を渡す手段を提供する。
flash に設定された値はすべて、次のアクションに公開され、その後クリアされる。
つまり、1リクエストだけリクエストを跨いで情報を保持することが可能なことを意味する。
以下はよく見るコードだけど、post の保存時に flash にメッセージを保持して show の画面で保存された post を get する際に保持していた flash の情報を使用しているということ。
code:ruby
class PostsController < ActionController::Base
def create
# save post
flash:notice = "Post successfully created" redirect_to @post
end
def show
# doesn't need to assign the flash notice to the template, that's done automatically
end
end
code:erb
<% # show.html.erb %>
<div class="notice"><%= flash:notice %></div> <% end %>