i have this 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', as:'user'
get '/sair', to:'login#destroy'
end
resources :login
resources :home
resources :produtos
resources :fornecedors
end
the Login controller:
class LoginController < ApplicationController protect_from_forgery
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
The home controller:
class HomeController < ApplicationController protect_from_forgery with: :exception
def index
@user = @user = User.find(session[:user])
@produtos = Produto.all
render 'inicio' end
def show
if session[:user]
@user = User.find(session[:user])
end end
end
i'm getting an error on the Home view (new.html.erb):
<header>
<h2><a href="#">Bem-vindo <%= @user.nome %></a></h2>
<nav>
undefined method `nome' for nil:NilClass
Why i have some problems with the session? I can do the login and i wanna see the user informations of this session on the redirected page, like if i can pass the @user variable assigned on the login action to the home controller to use it.
Aucun commentaire:
Enregistrer un commentaire