Root page of my app is post's index page
Rails.application.routes.draw do
root 'posts#index'
resources :users
resources :posts
resource :sessions, only: [:new, :create, :destroy]
resources :passwords, only: [:new, :create, :edit, :update]
end
My uses cookies to store user sessions. At the time of login, if the user checks the remember me checkbox, its session is remembered and he is able to access the app after closing the browser without needing to log in again.
Step I follow: 1. Run rails s on the terminal.
Type localhost:3000 on the browsers address bar
The app opens up,I land on posts#index page as it is root path my app follows.
I click on the login link, I fill email, password & check remember me checkbox
I land on user's profile page ie, users#show page
Terminal till this point
Now I close the browsers window. (I do not log out.)(cookies should rememeber my details)
I again open the browser. type localhost:3000 on browser's addreess bar.
I land 'posts#index' page. But I want to not on the user's profile ie, users#show page as my details are stored in cookies
But I want to land on the user's profile page.
On the application controller, if do...
before_action :set_cache_buster,:redirect_if_logged_in
def redirect_if_logged_in
redirect_to user_path(current_user) if logged_in?
end
And login if says redirect too many times
Application Controller:
# frozen_string_literal: true
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user,:logged_in?, :logged_in_user,:current_user
before_action :set_cache_buster
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.where("auth_token =?", cookies[:auth_token]).first if cookies[:auth_token]
end
def logged_in?
!current_user.nil?
end
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to new_sessions_path
end
end
def current_user?(user)
user == current_user
end
private
def set_cache_buster
response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
end
end
User Contoller
# frozen_string_literal: true
require 'resolv-replace'
class UsersController < ApplicationController
before_action :logged_in_user, only: [:show, :edit, :update]
before_action :correct_user, only: [:show, :edit, :update]
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(set_params)
if @user.save
UserNotifierMailer.send_signup_email(@user).deliver
flash[:success] ="Success"
redirect_to new_sessions_path
else
render 'new'
end
end
def show
@user = User.find(params[:id])
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update(update_params)
redirect_to @user
else
render 'edit'
end
end
private
def set_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def update_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
def correct_user
@user = User.find(params[:id])
rescue ActiveRecord::RecordNotFound
redirect_to(@current_user) unless current_user?(@user)
end
end
After I type localhost:3000 redirecting to 'post#index' is fine. but I add a conditional redirect. If cookies are not empty redirect to some other path.
I hope i am able to explain the problem well at least now.
You have redirect_if_logged_in
method called each time you are being redirected as your UsersController
inherits from ApplicationController
. As I understood you just need to move this code to PostsController
Try doing this, add before_action
before_action :check_logged_in
def check_logged_in
unless logged_in?
flash[:danger] = "Please log in."
redirect_to new_sessions_path
else
redirect_to user_path(current_user) if cookies[:auth_token]
end
end
You don't need logged_in_user
as helper method after adding this
Hope that helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.