I want to do more than one skip_before_action
tried this below option:
skip_before_action :user_logged_in, :user_is_provider, only: [:impressum, :agb, :help, :privacy]
but the
:user_is_provider
function isn't called yet. The function is in application_controller.rb
Thanks
skip_before_action :user_logged_in
skip_before_action :user_is_provider, only: [:impressum, :agb, :help, :privacy]
Related
I have a controller with the default RESTful route actions (index new create show edit update destroy) and also several other actions. I want to set a before_action that only runs on the default routes.
I know I can add before_action :set_x, only: [:index, :new, :create, :show, :edit, :update, :destroy] to the top of the controller but is there a quicker way to do this? I'd like to do this for multiple controllers, so I cannot do before_action :set_x, except: [:foo, :bar, :baz] because the actions change in each controller and new actions are added all the time.
Thanks!
Options:
define the before_action in the ApplicationController
write skip_before_action in those controllers that do not need it
define another "controller"-class, that derives from ApplicationController, add the before_action and only those controllers that need it, will inherit from that class
create a module with only the before_action and include it
I can check a user and to log out on some condition.
But there always a check on ApplicationController like a below.
before_action :authenticate_user!, :check_should_logout
I don't want to call check_should_logout on every request.
Is there any solution?
You can use only or except options for before_action filters in respective controller classes.
before_action :check_should_logout, only: [:action_1, :action_2]
OR
before_action :check_should_logout, except: [:action_1, :action_2]
before_action :authenticate_user!, :check_should_logout, only: [:action1, :action_2]
or
before_action :authenticate_user!, :check_should_logout, except: [:action1, :action_2]
Instead of providing this in ApplicationController tou should provide this in respective controller.
I am using this guide for setting up Devise in Rails:
http://codepany.com/blog/rails-5-user-accounts-with-3-types-of-roles-devise-rails_admin-cancancan/
It says to place this on your home controller to keep Devise from requesting authentication on your home page:
skip_before_action :authenticate_user!, :only => [:index]
My home controller is called dev so my dev_controller.rb looks like this:
class DevController < ApplicationController
def index
skip_before_action :authenticate_user!, :only => [:index]
end
end
Now when I visit my website, I get this error:
undefined method `before_action' for #<MenuController:0xb193b640>
Any ideas on why I get this error?
Please try the below
skip_before_action is should be out side of the index method scope. As before_action or skip_before_action is a class method. Should not call it inside in an instance method(index)
class DevController < ApplicationController
skip_before_action :authenticate_user!, :only => [:index]
def index
end
end
Please refer
As skip_before_action is a Class method, it can't be called from instance method.
It's also known as callback method, here are other methods.
You can update code as,
class DevController < ApplicationController
skip_before_action :authenticate_user!, :only => [:index]
def index
end
end
If skip_before_action seems to be confusing you can use before_action like below
before_action :authenticate_user! , except:[index]
I am using the before_action filter to call authenticate which is a method that will redirect users back to their home page if they aren't authorized to be on the page requested.
I would like to exclude a page from this step, just for testing purposes.
What I have seen so far is that I can use except to exclude certain controller actions from being subject to the before_action filter so like:
before_action :authenticate, except: :demo_login
I can also exclude more than one action at a time like this:
before_action :authenticate, except [:demo_login, :demo_show]
How can I exclude all actions in a specific controller?
Use skip_before_action :authenticate in the relevant controller.
The format of this method is the same as before_action so if you want to skip calling :authenticate for a specific controller action, use:
skip_before_action :authenticate, only: [:show, :index]
The except: keyword can also be used.
So, I have this code in a controller:
before_filter :require_login, :only => :new, :edit, :destroy
My controller has these methods: index, new, edit, create, update, show, destroy.
What I want to do is to protect with login_required (:require_login in the code) the methods: new, edit, destroy, but the above code doesn't work, I can protect one method if i have, for example:
before_filter :require_login, :only => :new
But I want to protect the three of them, How can I do it?
You're missing square brackets around the only option's value:
before_filter :require_login, :only => [:new, :edit, :destroy]
It's not working because the Ruby interpreter doesn't know where the options for only start and the arguments for before_filter continue. This is case where you need to be explicit about the container.
Use an array:
before_filter :require_login, :only => [:new, :edit, :destroy]
Oh I was a FOOL!!! it was just matter of putting them in array form -.- like this:
before_filter :require_login, :only => [:new, :edit, :destroy]
Sorry for the obvious question.