2017-06-06 29 views
-2

我呼吁登录以下控制器:“缺少必需的钥匙” - Ruby on Rails的

class LoginController < ApplicationController 
    skip_before_action :verify_authenticity_token 
    protect_from_forgery 
    before_action :require_user 
    helper_method :current_user 

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

    def require_user 
    redirect_to login_path unless current_user 
    end 

    def show 

    end 

    def new 
    if session[:user] 
     @user = User.find(session[:user]) 
    end 
    end 

    def destroy 
    reset_session 
    redirect_to "/login/acesso", notice: "Você foi deslogado" 
    end 

    def create 
    user = User.validate(login_params[:email], login_params[:senha]) 
    if user 
     session[:user] = user.id 
     redirect_to "/home/inicio", notice: "login feito com sucesso" 
    else 
     redirect_to "/login/acesso", notice: "Dados incorretos" 
    end 
    end 

    private 

    def login_params 
    params.require(:login).permit(:email, :senha) 
    end 
end 

,这是我的路线:

Rails.application.routes.draw do 

    root 'login#new' 
    get '/home/inicio', to: 'home#index' 

    scope '/login' do 
    get '/acesso', to:'login#new' 
    post '/acessorecebendo', to:'login#create' 
    get '/sair', to:'login#destroy' 
    end 

    resources :login 
    resources :home 
    resources :produtos 
    resources :fornecedors 
end 

和错误:

No route matches {:action=>"show", :controller=>"login"}, missing required keys: [:id]

在线:

def require_user 
    redirect_to login_path unless current_user 
end 

的一点是:如果我删除行 “before_action:...” 在登录控制器中,我得到这个错误:

Couldn't find User with 'id'=2

的new.html.erb(登录的视图):

<% if flash[:notice] %> 
    <div class="notice"><%= flash[:notice] %></div> 
<% end %> 

<div class="login-page"> 
    <div class="form"> 
    <form class="register-form" action="/login/acessorecebendo" method="post"> 
     <input type="text" name="login[email]" placeholder="Email"/> 
     <input type="password" name="login[senha]" placeholder="Senha"/> 
     <button>Cadastrar</button> 
     <p class="message">Já é registrado? <a href="#">Login</a></p> 
    </form> 
    <form class="login-form" action="/login/acessorecebendo" method="post"> 
     <input type="text" name="login[email]" placeholder="email"/> 
     <input type="password" name="login[senha]" placeholder="password"/> 
     <button>login</button> 
     <p class="message">Não está registrado <a href="#">Criar uma conta</a></p> 
    </form> 
    <% if session[:user] %> 
     <a href="/login/sair">Sair sessão <%= @user.nome %> </a> 
    <% end %> 
    </div> 
</div> 

因此,尝试解决这个问题,我需要做的方式来检查,如果有一个人登录与否并重定向到适当的视图。我试图做这样的事情之前,行动...

有人可以向我解释这一点,请? :\

我试图按照这种解决方案:Couldn't find User with id=1

但它没有工作..

+0

莫非你添加了将你重定向到show方法的视图? –

+0

什么看法?问题出在before_action方法:\ –

+0

@SebastiánPalma我把登录控制器的视图 –

回答

0

与此问题:

redirect_to login_path unless current_user 

那是login_path需要和id,这您可以通过在控制台中运行rake routes来查看:

  acesso GET /login/acesso(.:format)   login#new 
    acessorecebendo POST /login/acessorecebendo(.:format) login#create 
      sair GET /login/sair(.:format)   login#destroy 
     login_index GET /login(.:format)     login#index 
        POST /login(.:format)     login#create 
     new_login GET /login/new(.:format)    login#new 
     edit_login GET /login/:id/edit(.:format)  login#edit 
      login GET /login/:id(.:format)    login#show 
        PATCH /login/:id(.:format)    login#update 
        PUT /login/:id(.:format)    login#update 
        DELETE /login/:id(.:format)    login#destroy 
     home_index GET /home(.:format)     home#index 
        POST /home(.:format)     home#create 
     new_home GET /home/new(.:format)    home#new 
     edit_home GET /home/:id/edit(.:format)   home#edit 
        GET /home/:id(.:format)    home#show 
        PATCH /home/:id(.:format)    home#update 
        PUT /home/:id(.:format)    home#update 
        DELETE /home/:id(.:format)    home#destroy 

(我删除了一些为简洁起见。)

我猜你可能想要做:

redirect_to accesso_path unless current_user 

,或者可能:

redirect_to new_login_path unless current_user 

它们都指向同一个顺便说一句,所以你可能会摆脱accesso_path并用login_new取而代之。

所以,另一个问题听起来像你可能有(从你的意见),是你有一个无效的idsession[:user_id]。当您删除before_action :require_user,然后,你开始进入:

def new 
    if session[:user] 
    @user = User.find(session[:user]) 
    end 
end 

然后是提高例外:

Couldn't find User with 'id'=2

所以,在这里可能是你应该做的:

class LoginController < ApplicationController 
    skip_before_action :verify_authenticity_token 
    protect_from_forgery 
    before_action :go_home_if_signed_in 
    helper_method :current_user 
    # ^^^ 
    # I don't know what this is 


    def go_home_if_signed_in 
    if session[:user_id] 
     # there was a session[:user_id] 
     # use find_by instead of find because it won't raise an exception 
     if User.find_by(id: session[:user_id]) 
     # the session[:user_id] resulted in a valid user, so 
     # send the user to their home page. 
     redirect_to home_index_path 
     else 
     # if the User wasn't found, then you'll be in here, without 
     # raising an exception. 
     # So now you want to set the session[:user_id] to nil 
     # because it's invalid. 
     session[:user_id] = nil 
     end 
    end 
    end 

    # you should be able to get rid of this. 
    # def require_user 
    # redirect_to new_login_path unless current_user # or wherever, but not login_path 
    # end 

    def show 

    end 

    def new 
    # This seems wrong. What is session[:user] and how do you 
    # expect User to find it? 
    # if session[:user] 
    # @user = User.find(session[:user]) 
    # end 
    end 

    def destroy 
    # Make sure you set session[:user] = nil somewhere!!! 
    reset_session 
    # ^^^ 
    # I don't know what this is. 
    # redirect_to "/login/acesso", notice: "Você foi deslogado" 
    redirect_to :acesso_path, notice: "Você foi deslogado" 
    # ^^^ 
    # Why not use the path you generated in routes? You went through 
    # the trouble of defining it. 
    end 

    def create 
    user = User.validate(login_params[:email], login_params[:senha]) 
    if user 
     session[:user] = user.id 
     redirect_to "/home/inicio", notice: "login feito com sucesso" 
    else 
     redirect_to "/login/acesso", notice: "Dados incorretos" 
    end 
    end 

    private 

    def login_params 
    params.require(:login).permit(:email, :senha) 
    end 
end      
+0

我没有任何方法称为accesso_path或login_new。这不存在这里)= –

+0

不。这些是路径名称。看看我上面粘贴的“rake routes”的输出(第1行和第6行)。就像我说的,你正在尝试''redirect_to''' login_path'和那个路径需要和'id'(第8行)。顺便说一句,我得到'new_login_path'的路径名有点不对,我刚纠正了它。 – jvillian

+0

我明白,但我怎么能从这里“清除”缓存?我的意思是,如果我删除before_ation和是helper_method也是这两种方法也一样,我得到这个错误: 找不到用户与“ID” = 2 在该行: 高清新 如果会话[:用户] @user = User.find(session [:user]) end end –