Search code examples
ruby-on-railsrubyruby-on-rails-3devise

DEVISE sign in not working


Hi I'm very new to rails so I'm not sure what I'm doing wrong. I'm working on an app and have implemented very basic devise. The console output indicates that it should move on to the the page in the path when a sign is complete but it is never getting passed the sign in page.

User.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :sessions, dependent: :destroy
  has_many :session2s, dependent: :destroy
end

routes.rb

Rails.application.routes.draw do
# resources :users
devise_for :users
resources :sessions do 
  resources :session2s 
end
# The priority is based upon order of creation: first created -> highest    priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
root 'application#index'
get '/welcome'=>'application#index'
post'/sessions/user'=>'sessions#new'

rake routes

new_user_session GET    /users/sign_in(.:format)                           devise/sessions#new
        user_session POST   /users/sign_in(.:format)                           devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format)                          devise/sessions#destroy
   new_user_password GET    /users/password/new(.:format)                      devise/passwords#new
  edit_user_password GET    /users/password/edit(.:format)                     devise/passwords#edit
       user_password PATCH  /users/password(.:format)                          devise/passwords#update
                     PUT    /users/password(.:format)                          devise/passwords#update
                     POST   /users/password(.:format)                          devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)                            devise/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)                           devise/registrations#new
   edit_user_registration GET    /users/edit(.:format)                              devise/registrations#edit
       user_registration PATCH  /users(.:format)                                   devise/registrations#update
                         PUT    /users(.:format)                                   devise/registrations#update
                         DELETE /users(.:format)                                   devise/registrations#destroy
                     POST   /users(.:format)                                   devise/registrations#create
   session_session2s GET    /sessions/:session_id/session2s(.:format)          session2s#index
                     POST   /sessions/:session_id/session2s(.:format)          session2s#create
new_session_session2 GET    /sessions/:session_id/session2s/new(.:format)      session2s#new
   edit_session_session2 GET    /sessions/:session_id/session2s/:id/edit(.:format) session2s#edit
        session_session2 GET    /sessions/:session_id/session2s/:id(.:format)      session2s#show
                     PATCH  /sessions/:session_id/session2s/:id(.:format)      session2s#update
                     PUT    /sessions/:session_id/session2s/:id(.:format)      session2s#update
                     DELETE /sessions/:session_id/session2s/:id(.:format)      session2s#destroy
            sessions GET    /sessions(.:format)                                sessions#index
                     POST   /sessions(.:format)                                sessions#create
new_session GET    /sessions/new(.:format)                            sessions#new
        edit_session GET    /sessions/:id/edit(.:format)                       sessions#edit
             session GET    /sessions/:id(.:format)                            sessions#show
                     PATCH  /sessions/:id(.:format)                            sessions#update
                     PUT    /sessions/:id(.:format)                            sessions#update
                     DELETE /sessions/:id(.:format)                            sessions#destroy
                root GET    /                                                  application#index
             welcome GET    /welcome(.:format)                                 application#index
       sessions_user POST   /sessions/user(.:format)                           sessions#new

console messages

Started POST "/sessions/user" for 107.15.244.73 at 2017-04-30 03:24:53 +0000
Cannot render console from 107.15.244.73! Allowed networks: 127.0.0.1, ::1,    127.0.0.0/127.255.255.255
Processing by SessionsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"A8ZTEKOg+86FHiL7HMUo69Cz+0OmYiedg92Nci3D/ZhWwK1W1WN7weI/gZtq9Qc0yC1igj/ZpPwtLm6YkJhY9Q==", "user"=>{"email"=>"z@g", "password"=>"[FILTERED]", "remember_me"=>"1"}, "commit"=>"Log in"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)


Started GET "/users/sign_in" for 107.15.244.73 at 2017-04-30 03:24:53 +0000
Cannot render console from 107.15.244.73! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Devise::SessionsController#new as HTML
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/shared/_links.html.erb (1.1ms)
Rendered devise/sessions/new.html.erb within layouts/application (4.2ms)
Completed 200 OK in 26ms (Views: 25.1ms | ActiveRecord: 0.0ms)

I just created the username and password in signup and the logs show its definitely entered into the table so I'm not sure what's happening. I see the 401 error but all the previous questions on this topic have more complicated devise methods so I'm not sure whats going and help on getting a functioning sign in page would be great ! Thanks!

Update: Sessions Controller (please excuse the global variables I know its not great ruby syntax)

class SessionsController < ApplicationController
before_action :set_session, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
# GET /sessions
# GET /sessions.json
  def index
     @sessions = Session.all
     if user_signed_in?
       @user=User.find(current_user)
     end
  end

 # GET /sessions/1
 # GET /sessions/1.json
  def show
  end

 # GET /sessions/new
 def new
   @session = current_user.sessions.build
   @headers=$headers
 end

 # GET /sessions/1/edit
 def edit
 end

 # POST /sessions
 # POST /sessions.json
 def create
  @session = current_user.sessions.build(session_params)

  respond_to do |format|
    if @session.save
      format.html { redirect_to @session, notice: 'Session was successfully created.' }
      format.json { render :show, status: :created, location: @session }
    else
      format.html { render :new }
      format.json { render json: @session.errors, status: :unprocessable_entity }
    end
  end
  $filename1=session_params[:filename]
  $filename2=session_params[:filename2]
  $listbool1=session_params[:listbool1]
  $listbool2=session_params[:listbool2]
  if $filename1!=""
    [email protected]($filename1)
  end
  if $filename2!=""
    [email protected]($filename2)
  end
  if ($filename1!=""|| $filename2!="")
    [email protected]($header1,$header2,$listbool1,$listbool2)
  end  
end
# PATCH/PUT /sessions/1
# PATCH/PUT /sessions/1.json
def update
  respond_to do |format|
    if @session.update(session_params)
      format.html { redirect_to @session, notice: 'Session was successfully updated.' }
      format.json { render :show, status: :ok, location: @session }
    else
      format.html { render :edit }
      format.json { render json: @session.errors, status: :unprocessable_entity }
    end
  end
end
# DELETE /sessions/1
# DELETE /sessions/1.json
def destroy
  @session.destroy
  respond_to do |format|
    format.html { redirect_to sessions_url, notice: 'Session was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_session
    @session = Session.find(params[:id])
  end
 # Never trust parameters from the scary internet, only allow the white list through.
 def session_params
    params.require(:session).permit(:matchlist, :matchlist2,:filename, :filename2,:user_id, :headers,:listbool1,:listbool2)
  end
def session_owner
  unless current_user.id == @session.user_id
    flash[:notice]="Only the original recipe owner can delete sessions"
    redirect_to @session
  end
end

end


Solution

  • Most probably the controller name is messing up, as devise too has the sessions controller. Change the name of your controller and it should work. Even if it doesn't later, your authenticity token is failing probably, which is configured in your config variables. However, first try changing controller name.