I am new to ruby on rails. I am developing an application which has authentication system.
my problem is I am getting error when logging in to the application in production(Heroku). It is working in development.
Error
I production after i typing url https://akashpinnaka.herokuapp.com/login,
it is redirecting to https://akashpinnaka.herokuapp.comlogin. I am missing the '/' between root_url and 'login' for POST login.
Note: Working in development environment.
My routes are
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
Sessions 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
Sessions#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 %>
Usually, when your form can't be saved, you don't redirect. You show the same form, with error explanation.
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
If you are sure that you want to redirect, by all means, go ahead. But supply the correct path :)
You need use redirect_to '/login'
or redirect_to login_path
instead of redirect_to 'login'
@Sergio Tulentsev's answer is pretty good.
You should fix your routes:
#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 has two sets of path helpers - _path
and _url
_path
, as we know, is there to provide relative routes (/path
).
_url
is there to provide direct routes (http://domain.com/path
)
Thus, when you have:
get "/login"
(with leading slash) in your routes, it will almost certainly cause problems with your applications' relative link helpers.
As mentioned by @Sergio Tulentsev
, your create
method, and the destroy
method, should 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
It's worth taking @Sergio
's advice on the render :new
command too!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With