2016-11-22 34 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