Home > OS >  Failed to redirect from login_page to home page using devise
Failed to redirect from login_page to home page using devise

Time:06-20

Hope you all are doing well. Actually i am facing an issue regarding redirect of my application from one page to another page.

Here is my sessions controller:-

```class Users::SessionsController < Devise::SessionsController
  def new
    @user=User.new 
  end

  def create
    @user=User.find_by(email: params[:email])
    if user.present? && user.authenticate(params[:password])
        session[:user_id] = user.id
        redirect_to home_path, notice: "Logged in Successfully"
    else
        flash[:alert] = "Invalid Email or Password"
        render :new
    end
  end  
  def destroy
    session[:user_id] = nil
    redirect_to root_path, notice: "Logged Out"
end  
end```

My session view

```<div >
<!-- Outer Row -->
<div >

<div >

<div >
<div >
<!-- Nested Row within Card Body -->
<div >
  <div ></div>
  <div >
      <div >
          <div >
              <h1 >Log In!</h1>
          </div>
        <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
         <%= render "devise/shared/error_messages", resource: resource %>
        <div >
        <%= f.email_field :email, autofocus: true, autocomplete: "email", class:"form-control form-control-user", placeholder:"Enter Email Address..." %>
        </div>
        <div >
        <%= f.password_field :password, autofocus: true, autocomplete: "current-password", class:"form-control form-control-user", placeholder:"Please Enter Password" %>                                 </div>
        </div>
        <div >
        <div >
        <% if devise_mapping.rememberable? %>
        <div >
        <%= f.check_box :remember_me %>
        <%= f.label :remember_me %>
        </div>
        
        <% end %>
      </div>
      <div >
      <%= f.submit "Log in", class:"btn btn-primary btn-user"%><br>&nbsp;&nbsp;&nbsp;
      </div>
 
      <% end %>
          </div>
      </div>
    </div>
  </div>
  </div>
  </div>
  </div>
  </div>
  </div>```

def create
    @user=User.find_by(email: params[:email])
   if users.present? && users.authenticate(params[:password])  
        session[:user_id] = user.id
        redirect_to home_path, notice: "Logged in Successfully"
    else

In this last error code i am facing issue in 3rd line user.present? line. Please help me in this it's been 2 days am working on this issue but no working

CodePudding user response:

You don't need to override Devise::SessionsController in your case. Just remove

controllers: { sessions: 'users/sessions' }

from your routes. Should be just:

devise_for :users

And add after_sign_in_path_for to your ApplicationController controller

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(*)
    home_path
  end
end
  • Related