i have the following Controller called Login:
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
and this is my routes:
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
and the error:
No route matches {:action=>"show", :controller=>"login"}, missing required keys: [:id]
at the lines:
def require_user redirect_to login_path unless current_user end
the point is: if i delete the line "before_action :..." found in Login controller, i got this error:
Couldn't find User with 'id'=2
so, to try solve this, i need to do a way to check if are someone logged or not and redirect to the proper view. I'm trying do something like that with this before action... can someone explain this to me, please? :\
i tried to follow this solutions: Couldn't find User with id=1
but didnt work..
Aucun commentaire:
Enregistrer un commentaire