POST login does not work in production

I'm new to rubies on rails. I am developing an application that has an authentication system.
my problem is that I get an error when entering the application into production (Heroku). He works in development. Error
I production after entering the text https://akashpinnaka.herokuapp.com/login , it redirects to https: //akashpinnaka.herokuapp.comlogin . I miss the "/" between root_url and "login" to log into POST.
Note . Work in a development environment.

My routes

Rails.application.routes.draw do

get 'welcome/index'
root 'welcome#index'
resources :articles
resources :projects
resources :users

get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'

end

Session Controller

class SessionsController < ApplicationController

  def new
  end

  def create
    @user = User.find_by_email(params[:session][:email])
    if @user && @user.authenticate(params[:session][:password])
      session[:user_id] = @user.id
      redirect_to root_path
    else
      redirect_to 'login'
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to '/'
  end

end

Session # new

<%= form_for(:session, url: login_path) do |f| %> 
  <%= f.email_field :email, :placeholder => "Email" %> 
  <%= f.password_field :password, :placeholder => "Password" %> 
  <%= f.submit "Log in" %>
<% end %>
+4
3

, , . .

  def create
    @user = User.find_by_email(params[:session][:email])
    if @user && @user.authenticate(params[:session][:password])
      session[:user_id] = @user.id
      redirect_to root_path
    else
      # should've been login_path
      # redirect_to 'login'

      render 'new' # this is better
    end
  end

, , , . :)

+3

redirect_to '/login' redirect_to login_path redirect_to 'login'

@ .

+2

:

#config/routes.rb
root "welcome#index"
resources :articles, :projects, :users
resources sessions, only: [:new, :create, :destroy], path_names: { new: "login", create: "login", destroy: "logout" } 

Rails - _path _url

_path, , (/path).

_url (http://domain.com/path)

, :

get "/login" ( ) , .


As mentioned @Sergio Tulentsev, your method createand method destroymust be fixed to use the correct path helpers:

def create
    @user = User.find_by email: params[:session][:email]
    if @user && @user.authenticate(params[:session][:password])
       session[:user_id] = @user.id
       redirect_to root_path
    else
       redirect_to login_path
    end
end

def destroy
    ...
    redirect_to root_path
end

For a team render :newworth taking @Sergio!

+1
source

Source: https://habr.com/ru/post/1621692/


All Articles