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
?
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.
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.
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.
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.
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.
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