How to set custom flash with respond_to in Rails
Asked Answered
T

2

5

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?

Teraterai answered 18/12, 2012 at 15:30 Comment(0)
S
8

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

Seat answered 18/12, 2012 at 15:35 Comment(0)
E
5

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!

Electrobiology answered 16/12, 2014 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.