> For the complete documentation index, see [llms.txt](https://kelby.gitbook.io/rails-beginner-s-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kelby.gitbook.io/rails-beginner-s-guide/actioncontroller/outside_the_metal/flash.md).

# Flash

Action Controller 和 Action Dispatch 都有 Flash 相关的代码。

## 基本使用

类似 Hash，设置 flash

```ruby
class PostsController < ActionController::Base
  def create
    # save post
    flash[:notice] = "Post successfully created"
    redirect_to @post
  end

  def show
    # ...
  end
end
```

类似 Hash, 读取 flash

```ruby
# show.html.erb
<% if flash[:notice].present? %>
  <div class="notice"><%= flash[:notice] %></div>
<% end %>
```

## alert 和 notice

因为 alert 和 notice 类型的 flash 太常见，所以提供了语法糖，你还可以这么写：

```ruby
# 设置
flash.alert = "You must be logged in"
flash.notice = "Post successfully created"

# 读取
flash.alert
flash.notice
```

> Note: 其它 flash\_type 默认不能这么写

上面是由 Action Dispatch 提供，下面由 Action Controller 提供。

## add\_flash\_types 方法

觉得上面的写法还是不够简短，觉得 notice 和 alert 类型不够用？使用 `add_flash_types`

```ruby
# app/cotrollars/pplication_controller.rb
class ApplicationController < ActionController::Base
  add_flash_types :warning, :success, :danger
end

# View 代码
<%= warning %>

# Controller 代码
redirect_to user_path(@user), warning: "Incomplete profile"
```

两种效果：视图里可以直接有同名 `warning` 辅助方法，`redirect_to` 里可直接使用 `:warning`.

它们和 flash\[:warning] 或 flash.warning 和 flash: { warning: "Incomplete profile" } 效果一样。

## 还是 alert 和 notice

alert 和 notice 默认已经使用 add\_flash\_types

经验：默认 Rails 提供了上述类型的 flash，实际情况中一般是不够用的（至少对应红、黄、绿）3种级别的消息。所以，建议您用同样的方法添加自己的 flash.

## flash.now\[:flash\_type]

也许，你还看过一种写法 `flash.now[:flash_type]`

flash\[:flash\_type] 消息的生命周期可到下一个 action. 所以，通常搭配 redirect\_to 使用。\
flash.now\[:flash\_type] 消息的生命周期仅限于本 action. 所以，通常搭配 render 使用。

以 update 为例：如果成功则跳转到新页面，那么可用 flash\[:flash\_type]; 失败则停留在当前页面，那么可用 flash.now\[:flash\_type].

另，当你意外的发现提醒消息在一个页面出现，在下一个页面还是出现，不妨改为 flash.now\[:flash\_type] 试试。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kelby.gitbook.io/rails-beginner-s-guide/actioncontroller/outside_the_metal/flash.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
