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