How to redirect after login - Rails

Asked

Viewed 411 times

0

Routes:

Rails.application.routes.draw do

  get 'home/inicio'
  root 'login#new'

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

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

end

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">
      <input type="text" placeholder="username"/>
      <input type="password" 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>

Login controller:

class LoginController < ApplicationController
  skip_before_action :verify_authenticity_token

  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 page is the login and if it successfully validates, it will redirect to another page rendered by the controller home start action. I don’t know how to do it =\

  • You are wearing Devise?

  • No. but I wouldn’t know how to use it either

  • Hi @Mikhaelaraujo use Devise https://github.com/plataformatec/devise just install Gem and run the views and controllers creation rails generate devise:install continue reading https://github.com/plataformatec/devise/blob/master/README.md is worth a lot and its application with Devise will be much safer

  • @Glaucoroberto sounds interesting but this is for a small college job and I won’t delve into it.

  • Another reason to use Devise. Greatly simplifies your work.

  • What mistake do you get by trying to make that way there?

  • You’re using a reserved model word called "validate". You would first have to exchange this word, then see how you are doing the search in the user model.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.