Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom flash with respond_to in Rails

In respond_to you can set flash[:notice] like this

respond_to do |format|
  format.html { redirect_to photo_path(photo), :notice => 'The photos was saved') }
  format.xml  { render :xml => photo, :status => :created}
end

I am trying to set flash[:success] with :success => "yay" but it doesn't work.

Am I doing something wrong?

like image 928
Nick Ginanto Avatar asked Dec 18 '12 15:12

Nick Ginanto


2 Answers

You should use redirect_to differently :

redirect_to photo_path(photo), :flash => { :success => "Yeepee!" }

The only flashes you can use directly are

  • :notice
  • :alert
  • :error

Hope that helps

like image 57
Kzu Avatar answered Sep 22 '22 08:09

Kzu


From Rails 4, you can directly use :success in redirect_to.

Just add this line:

# in app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
    [...]

    add_flash_types :error, :success, :info

    [...]

Without this line, in respond_to, :notice produces flash, but :success doesn't work.

Hat tip to Milan Mondal's post for this!

like image 34
jon Avatar answered Sep 24 '22 08:09

jon