jpe
jpe

Reputation: 11

Rails Devise - prevent redirect to log in from root

I want users to access the home page whether they're authenticated or not. Though, whenever the root url is visited, the user is immediately redirected to /users/sign_in. How do I disable this?

routes.rb

Rails.application.routes.draw do

  root 'home#index'
  devise_for :users

end

home_controller.rb

class HomeController < ApplicationController
  before_action :authenticate_user!, except: [:index]
  
  def index
  end


end

Upvotes: 1

Views: 544

Answers (1)

max
max

Reputation: 102164

With Devise is usually better to require authentication by default and then add exceptions with skip_before_action:

class ApplicationController < ActionController::Base 
  before_action :authenticate_user!
end

class HomeController < ApplicationController
  skip_before_action :authenticate_user!
 
  def index
  end
end

This avoids the risk of leaving security holes just by programmer omission.

Upvotes: 1

Related Questions