i've some problem with the skip_before action:
class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :require_login before_action :inc_cookies def inc_cookies if cookies[:token] != nil @name = cookies[:name] @surname = cookies[:surname] @user_roomate = cookies[:roomate] end end def require_login if cookies[:token] == nil puts "No token" redirect_to '/' end end end
and my other controller:
class UsersController < ApplicationController skip_before_action :require_login, :except => [:landing, :connect, :create] end
I don't know why, but when I'm on the root (the :landing action from UsersController), Rails try to pass in the require_login... I've misundertood something with this filter, or do I something wrong?
Thanks for any help!
Filters (before_action, skip_before_action) 2020 The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter renders or redirects, the action will NOT run.
In the Rails architecture, Action Controller receives incoming requests and hands off each request to a particular action. Action Controller is tightly integrated with Action View; together they form Action Pack. Action Controllers, or just “controllers,” are classes that inherit from ActionController::Base .
When writing controllers in Ruby on rails, using before_action (used to be called before_filter in earlier versions) is your bread-and-butter for structuring your business logic in a useful way. It's what you want to use to "prepare" the data necessary before the action executes.
Filters are methods that are run "before", "after" or "around" a controller action. Filters are inherited, so if you set a filter on ApplicationController , it will be run on every controller in your application.
This sounds normal to me - you've asked rails to skip your before action, except if the action is :landing
, :connect
or :create
whereas it sounds as though you want the opposite. If you want those 3 actions not to execute the require_login
then you should be doing
skip_before_action :require_login, :only => [:landing, :connect, :create]
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