2017-06-06 69 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 –