Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails skip before action doesn't work

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!

like image 892
Julien Leray Avatar asked Nov 29 '14 15:11

Julien Leray


People also ask

What does skip_ before_ action do?

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.

What is ActionController in Rails?

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 .

What is before action in Rails?

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.

What are Filters in Ruby?

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.


1 Answers

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] 
like image 130
Frederick Cheung Avatar answered Sep 21 '22 06:09

Frederick Cheung