---
# System prepended metadata

title: '[Ruby On Rails][controller]使用flash傳遞一次性的訊息(配合bootstrap 4)'
tags: [Ruby On Rails]

---

# [Ruby On Rails][controller]使用flash傳遞一次性的訊息(配合bootstrap 4)

###### tags: `Ruby On Rails`

參考資料:
https://ihower.tw/rails/actioncontroller.html#sec9


## controller
```ruby

 def create
    @product=Product.new(product_params)

    if @product.save
      flash[:notice]="new product created：#{@product.name}"
      redirect_to products_path
    else
      render "new"
    end
  end
```
## view(使用bootstrap 4)
```rust!
#...略
<% if flash[:notice]%>
          <div class="alert alert-warning alert-dismissible fade show" role="alert">
              <strong>new message</strong>  
              <%=flash[:notice]%>
              <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
          <% end %>    

```



