Reputation: 20445
I'm using Devise for my Rails 3 app.
How do I turn off Devise's alert messages for sign on/off successfully?
Upvotes: 10
Views: 4733
Reputation: 9430
Building on what others said, this way might be a little easier than having to extend devise or anything like that.
Make sure to use empty strings instead of removing the whole line, otherwise devise will just go back to using the default value for that message.
# blank out any string you don't want to render as a message
devise:
failure:
already_authenticated: ''
unauthenticated: ''
unconfirmed: ''
...
Now devise will still pass the empty string as a flash alert. But now it will look something like this with the message being an empty string
#<ActionDispatch::Flash::FlashHash:0xa7647c4
@closed=false,
@flashes={:alert=>""},
@now=nil,
@used=#<Set: {:alert}>>
I use a helper method in my ApplicationHelper file that handles collecting all the messages together. You may do this differently, but this will give you the idea.
def all_messages
# Standard flash messages
messages = flash.map{|key,val| {:type=>key, :message=>val} unless val.blank? }.compact
# |-------------------------|
# This is where the magic happens. This is how I ignore any blank messages
# Model validation errors
model = instance_variable_get("@#{controller_name.singularize}")
unless model.nil?
messages += model.errors.full_messages.map do |msg|
{:type=>:error, :message=>msg}
end
end
return messages
end
And voila, the unless val.blank?
statement maps any blank value to nil, and the .compact
method will remove any nil values, leaving you with a squeaky clean array with no blank messages.
Upvotes: 0
Reputation: 19203
You can either:
Go to config\locales\devise.en.yml and change the lines you want to empty strings (deleting them won't work). So, like this:
sessions:
signed_in: ''
signed_out: ''
Or extend/override devise's sessions controller. To do this, copy the create
and destroy
actions code from here, and paste it in a controller (let's call it sessions) that inherits from devise's sessions controller, like this:
class SessionsController < Devise::SessionsController
Then remove the calls to set_flash_message. Finally, edit your routes file so this change takes effect:
devise_for :users, :controllers => { :sessions => 'sessions' }
Upvotes: 18