Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before_filter with another controller

I am trying to create an action that will check for every page if the user is logged in. For that, in the controller Home I created this method:

def check_session
  if !session[:user_id]
    redirect_to :action=> 'login'
  end
end

And I've put this code at the head of the controller:

before_filter :check_session, :except => [:sub_layout, :authenticate, :login]

Now I want to use check_session from outside the pages of Home, lets say in the pages of Users. What is the correct syntax for calling a method of a different controller in the before_filter?

like image 837
hizki Avatar asked Jan 13 '11 02:01

hizki


People also ask

Can one model have multiple controllers?

Multiple controllers act on and transform the underlying model by interacting via one or more views. A single model is transformed to multiple synchronous views.

What do Rails controllers do?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.

What is an action controller?

Action Controller is the C in MVC. After the router has determined which controller to use for a request, the controller is responsible for making sense of the request and producing the appropriate output.

What does before_ filter do Rails?

before_filter and around_filter may halt the request before a controller action is run. This is useful, for example, to deny access to unauthenticated users or to redirect from HTTP to HTTPS. Simply call render or redirect. After filters will not be executed if the filter chain is halted.


1 Answers

If you move everything you already have to the application_controller it will check for every controller in your app. Then use the :skip_before_filter method to bypass the check for whatever controllers/actions you want.

like image 110
JackCA Avatar answered Sep 22 '22 13:09

JackCA