2016-11-22 33 views
0

我做了一個簡單的應用程序與數據庫交互,驗證和授權用戶之後,可以執行CRUD操作。它曾經工作時,有一個單一的表,但只要我開始玩嵌套關係和認證機制,我得到的錯誤。到目前爲止,我認爲我已經做好了一切。下面是錯誤的截圖:Ruby on Rails的拋出錯誤wihen重定向到root_path或login_path

enter image description here

這裏是我的routes.rb:

Rails.application.routes.draw do 
root to: "todo_lists#index" 

    resources :todo_lists do 
    resources :todo_items 
    end 

    resources :sessions, only: [:new, :create, :destroy] 
    get "/login" => "sessions#new", as: "login" 
    delete "/logout" => "sessions#destroy", as: "logout" 
end 

我的會話控制器:

class SessionsController < ApplicationController 
     def new 
     end 

     def create 
     @user = User.find_by(username: params[:username]) 
     @password = params[:password] 
     end 

     if @user && @user.authenticate(password) 
     session[:user_id] = @user.id 
     redirect_to root_path, notice: "Logged in successfully" 
     else 
     redirect_to login_path, alert: "Invalid Username/Password combination" 
     end 

     def destroy 
     reset_session 
     redirect_to login_path, notice: "You have been logged out" 
     end 
    end 

哦,我修改了ApplicationController中因此路線應儘管如此工作:

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 

    before_action :ensure_login 
    helper_method :logged_in?, :current_user 

    protected 
     def ensure_login 
     redirect_to login_path unless session[:user_id] 
     end 

     def logged_in? 
     session[:user_id] 
     end 

     def current_user 
     @current_user ||= User.find(session[:user_id]) 
     end 
end 

回答

0

我認爲的代碼是不是在create方法,並root_path是一種實例方法:

class SessionsController < ApplicationController 
    def new 
    end 

    def create 
    @user = User.find_by(username: params[:username]) 
    @password = params[:password] 
    end # <--- here create method finish. Where this end comes from? 

    if @user && @user.authenticate(password) 
    session[:user_id] = @user.id 
    # root_path is defined for an instance 
    # of a controller, not for the class 
    redirect_to root_path, notice: "Logged in successfully" 
    else 
    redirect_to login_path, alert: "Invalid Username/Password combination" 
    end 

    def destroy 
    reset_session 
    redirect_to login_path, notice: "You have been logged out" 
    end 
end 
+0

謝謝,這對我來說太無聊了。 –

-1

您是否嘗試過使用root_url,而不是root_pathSessionsController?做302重定向時

HTTP需要一個完全合格的URL。 _url方法提供絕對路徑,包括協議和服務器名稱。 _path方法提供了一個相對路徑,同時假定與當前URL相同的服務器和協議。

嘗試切換到root_url,讓我知道如果有什麼變化!

編輯:我建議使用絕對路徑的原因是因爲你提到重定向工作正常,只有一個表,在添加另一個表和嵌套路由之前。

編輯:我注意到的另外一件事是下面的驗證塊不在sessions#create路徑中,正如您所期望的那樣。

if @user && @user.authenticate(password) 
    session[:user_id] = @user.id 
    redirect_to root_path, notice: "Logged in successfully" 
else 
    redirect_to login_path, alert: "Invalid Username/Password combination" 
end 

是否有一個原因存在於方法之外?嘗試將其移回到創建路徑中。

+0

試過了。使用_url獲取相同的錯誤。 –

+0

好的。你也可以嘗試在'SessionsController'中添加'include Rails.application.routes.url_helpers'。如果這不起作用,我會繼續尋找!其他 – Gundam194

+0

有一件事我就是注意到,用戶認證模塊 – Gundam194

0

您的重定向邏輯的方法塊外:

if @user && @user.authenticate(password) 
    session[:user_id] = @user.id 
    redirect_to root_path, notice: "Logged in successfully" 
    else 
    redirect_to login_path, alert: "Invalid Username/Password combination" 
end 

該代碼應該是前create方法的最後end