I'm trying to set up a sign in form on my home page. I managed to do it by following the Wiki
Except, if the login infos are incorrect, the /devise/session/new.html.erb
is rendered.
I don't want that. I want my user to be redirected to the root_url
WITH the errors in a flash message.
I was able to override registrations_controller.rb
for another feature but override sessions_controller.rb
gives me a lot of trouble.
What am I suppose to change to do what I want ? I still want the user to be redirected to after_sign_in_path
if the sign in went right.
The original controller is here
So far, I know I have to do that in my routes.rb
devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions" }
And I also set up controller/sessions_controller.rb
class SessionsController < Devise::SessionsController
# GET /resource/sign_in
def new
resource = build_resource
clean_up_passwords(resource)
respond_with_navigational(resource, stub_options(resource)){ render_with_scope :new }
end
end
I have the feeling that I have to change the render_with_scope :new
but ...how?
With that version, I get the error undefined method 'users_url' for #<SessionsController:0x5072c88>
Well, I'll wait for your precious help
PS: That post helped me a lot for handling errors on subscription. Maybe it'll help for sign in errors?
==== EDIT ====
Following the 1rst answer advice, I also added to initializers/devise.rb
:
config.warden do |manager|
manager.failure_app = CustomFailure
end
When the user is not logged and tries to access a "restricted" area, he gets redirected to root_url
but when the sign in goes wrong, I now have the following error :
The action 'devise/sessions#new' could not be found for Devise::SessionsController
(PS: I deleted everything I did with Session Controller and the log in works if successful)
=== EDIT 2 ===
Following this Wiki the redirection works perfectly BUT I don't have any error notification.
And I'm displaying the alert/notice flash message with that if that changes anything
<% flash.each do |name, msg| %>
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :p, message, :id => "flash_#{name}" %>
<% end %>
<% else %>
<%= content_tag :p, msg, :id => "flash_#{name}" %>
<% end %>
<% end %>
=== FINAL UPDATE ===
The accepted answer works. Just don't forget to display flash alerts