Rails before_filter for specific actions in controller
Create in your ApplicationController
method:
def check_privileges!
redirect_to "/", notice: 'You dont have enough permissions to be here' unless current_admin || current_company
end
And then in your controller:
before_filter :check_privileges!, only: [:new, :create, :edit, :save]
Or
before_filter :check_privileges!, except: [:index, :show]
How to apply before_filter to every action of every controller in Rails 3.2.11?
To ensure that filters apply to all actions, place it in the application_controller.rb.
before_filter: is it possible to specify controller for action?
Options:
Use a skip_before_filter in the UsersController
skip_before_filter :login_required, :only => :show
Applying the before_filter individually to each controller, if your exceptional cases grow.
Use action from a controller as filter in another controller in rails
@NickM has covered this in his comment... have OtherController
inherit from PermissionController
class PermissionController < ApplicationController
def authorize
...
end
end
class OtherController < PermissionController
before_filter :authorize
end
However I note your authorize method has a parameter?
You'll need to handle that in the before_filter clause. Assuming you can store permission_id
in a session variable...
class PermissionController < ApplicationController
def authorize(permission_id)
...
end
end
class OtherController < PermissionController
before_filter { |controller| controller.authorize(session[:permission_id] }
end
Rails use not condition with before_filter
You could pass a block to before_filter
:
before_filter { |c| !c.logged_in? }
But this wouldn't really do anything, since the return value from the before filter isn't going anywhere. If you want to execute an action if a user is not logged in, then you should be putting that action into the before_filter.
For example, if the action was to redirect to the login page, path, you could do this:
before_filter { |c| redirect_to login_path unless c.logged_in? }
That's actually long enough to justify a method of its own:
before_filter :login_required
def login_required
redirect_to login_path unless logged_in?
end
In rails, does a method called in the before filter of a controller run for every action?
before_filter
will call the method for every request to the controller. You can set it to run only for some actions:
before_filter :authorize, :only => :delete
Or even prevent it from running in particular actions:
before_filter :authorize, :except => [:index, :show]
BTW, the new syntax for before_filter
is before_action
Do before_filter :except work across subclass controller?
Normally this should work. Make sure you are using symbols, as @jdl is saying.
Most of the times we use a different approach though. In your ApplicationController
you write
before_filter :authorize
So that by definition all access is authorized. In your UsersController
you can then add an exception:
skip_before_filter :authorize, :only => [:index, :show, :different_controller_method]
Hope this helps.
Related Topics
How to Access Class Variables in Ruby 1.9
Strictly Convert String to Integer (Or Nil)
Spork 0.9.2 and Rspec 3.0.0 = Uninitialized Constant Rspec::Core::Commandline (Nameerror)
Ruby: Write Escaped String to Yaml
Check If a File Exists Using a Wildcard
Rails: Why Images Are Not Showing in My Rails Basic App
How to Profile Ruby 1.9.2 Scripts with Memory Allocation Reports
Other Ruby Map Shorthand Notation
Convert String to Class Name Without Using Eval in Ruby
Is Alias_Method_Chain Synonymous with Alias_Method
Using Gets() Gives "No Such File or Directory" Error When I Pass Arguments to My Script
Dynamically Creating Class in Ruby
Why Is Date.Today - 6.Months + 6.Months != Date.Today
Ruby/Ror: Calling Original Method via Super()