Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails flash messages - :alert :error :notice and :success?

In several of my controllers, I have redirects/flash messages

redirect_to products_url, :notice => "message here",  redirect_to states_url, :error => "oops!" etc...  

In my sessions controller, however, upon successful authentication, I have flash[:success] = "welcome!" redirect_to user

I'd like to be able in my other controllers to do something like :success => "yay!"

This is mostly for cosmetic/consistency purposes, but are :notice, :alert and :error the only flash-types available / can I add additional types? Am I making sense?

Thanks!

like image 725
Chip Avatar asked Sep 28 '11 04:09

Chip


People also ask

How does flash work in Rails?

Per the Rails Docs, flash is a middleware method that sends forward temporary datatypes which the Rails controller delegates to the request object. Now in plain English: flash is a method through which you can send a temporary string, array, or hash once between your otherwise stateless HTTP requests.

What is flash messaging service?

Flash SMS is a distinct kind of text message that appears instantly on the screen of a mobile device without requiring the user to take any action in order to read it. Even if the screen is locked, a flash SMS, also referred to as a class 0 SMS, will display on the smartphone.


2 Answers

I believe without changes, this is as close as you'll get:

redirect_to user_path(@user), :flash => { :success => "Message" } 

Here's some additional notes regarding the friendly flash syntax addition.

like image 198
tlbrack Avatar answered Sep 18 '22 12:09

tlbrack


I just found out that in Rails 4 you can register custom types in app controller:

class ApplicationController     ...   add_flash_types :error, :another_custom_type end  # app/controllers/users_controller.rb class UsersController < ApplicationController   def create     ...     redirect_to home_path,       error: "An error message for the user"   end end  # app/views/home/index <%= error %> 

The merit goes to http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013

like image 29
luigi7up Avatar answered Sep 18 '22 12:09

luigi7up