Reputation: 29
I'm still learning rails so i guarantee my problem is really simple.
My site has 3 controllers; Scripts, Users and Pages. My problem is if a user isn't signed in they are automatically sent straight to the login/sign up page due to my before action on my Scripts controller. I don't want this to happen when a user is on my root page which is associated to my Pages controller?
before_action :set_script, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
Ive tried experimenting with but I cant seem to figure it out
before_action :set_script, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:new, :show, etc]
Thanks Stack overflow
Also this is my log for rails s
Processing by PagesController#index as HTML
Completed 401 Unauthorized in 8ms (ActiveRecord: 0.0ms | Allocations: 1163)
Started GET "/users/sign_in" for 127.0.0.1 at 2020-05-06 20:34:21 +1000
Processing by Devise::SessionsController#new as HTML
Rendering devise/sessions/new.html.erb within layouts/application
Rendered devise/shared/_links.html.erb (Duration: 1.6ms | Allocations: 713)
Rendered devise/sessions/new.html.erb within layouts/application (Duration: 11.2ms | Allocations: 3029)
[Webpacker] Everything's up-to-date. Nothing to do
Rendered layouts/_nav.html.erb (Duration: 0.7ms | Allocations: 452)
Rendered layouts/_messages.html.erb (Duration: 0.4ms | Allocations: 204)
Rendered layouts/_footer.html.erb (Duration: 0.3ms | Allocations: 79)
Completed 200 OK in 104ms (Views: 86.1ms | ActiveRecord: 6.2ms | Allocations: 22723)
Upvotes: 1
Views: 1244
Reputation: 1012
You should leave the before_action :authenticate_user!
, to make by default all your controller actions not public
If you need in some controller to make an action public, then in the specific controller, so your PagesController
, you can skip a before_action
with something like
class PagesController < ApplicationController
skip_before_action :authenticate_user!, only: :home
def home
end
end
This will make the home
action public
Upvotes: 0
Reputation: 29
I had to omit before_action :authenticate_user! from my application controller
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end
to
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
end
end
Upvotes: 1