lundi 31 juillet 2023

Can't able to create a simple login page in Rails

routes.rb

Rails.application.routes.draw do
  get '/login' , to: 'sessions#new'
  post '/login', to: "sessions#create"
end

sessions_controller.rb

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to user
    else
      flash.now[:danger]='Invalid login id'
      render 'new'
    end
  end

  def destroy
    log_out
    redirect_to root_url
  end
end

views/sessions/new.html.erb

<%= form_with url: login_path, local: true do |form| %>
    <%= form.label :email %>
    <%= form.email_field :email %>
    <%= form.label :password %>
    <%= form.password_field :password %>
    <%= form.submit "LogIn"%>
<% end %>

Error: NoMethodError in SessionsController#create undefined method `[]' for nil:NilClass Extracted source (around line #6): 5.def create 6. user = User.find_by(email: params[:session][:email].downcase) 7. if user && user.authenticate(params[:session][:password])

I still can't understand why is this happening.

tried everything to solve on my own but can't.

Aucun commentaire:

Enregistrer un commentaire