Rails 4: before_filter vs. before_action
As we can see in ActionController::Base
, before_action
is just a new syntax for before_filter
.
However the before_filter
syntax is deprecated in Rails 5.0 and will be removed in Rails 5.1
Not sure if I am using before_filter/before_action correctly in Rails
You can call before_filter
only for some specific controller action
like following
before_filter :user_has_food, only: [:display_user_food]
In rails 5.0
before_filter
is deprecated and you should use before_action
. And if you have only one action to filtered, you can avoid using array, see the bellow snippet
before_filter :user_has_food, only: :display_user_food
When you use only
option in before_filter
it will ignored calling while request comes for other action.
Rails before_filter and action identification
The action_name
method on the controller should give you what you're looking for. It's not documented, though, so there is no guarantee it won't disappear someday.
before_filter { |controller| logger.debug "Running before the #{controller.action_name} action" }
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]
execute before_filter/before_action for all subresources
You can define that in a concern and you can include that concern in whichever child controller you want to display the message in.
before_filter not being called Rails 4
You might want to try using before_action
instead of before_filter
.
edit: as paul richter mentioned in the comments... before_filter
will still work. But be aware that Rails is discouraging the use of before_filter
and suggesting developers to use before_action
instead.
source: Ruby on Rails 4.2 Release Notes
before_filter in ApplicationController not called (Rails 4)
//edit:
grml. i saw you already fixed your problem.
class ApplicationController < ActionController::Base
before_filter :throw_up
before_filter {
raise "hi friend"
end
private
def throw_up
raise "i need to puke"
end
end
class PagesController < ApplicationController
def index
end
end
if i call the index action - its working perfectly.
it is raising "i need to puke".
please do me a favor: if you write methods called by a filter, they should be private.
Is proc required with conditional before_action/before_filter?
Found it on Rails Guides: http://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks
Turns out a Proc
isn't always required for it to work.
the
:if
and:unless
options, which can take a symbol, a string, aProc
or anArray
.
So in your case you could probably get away with
before_action :check_stuff, if: "Rails.env.production?"
Finding things in Rails documentation can be a pain sometimes, but at least questions like this make things easier to find over time since StackOverflow is well indexed and has high search rankings.
How before_filter work in rails?
What it is important to know is that the filters can stack. I mean, if
WheteverController < ApplicationController
If you create a filter in the ApplicationController this will be executed before any method in your WheteverController. That is great if you want to authorize all the application with one line.
By the way, the before_filters can receive a paramenter:
- only [:methods_affected]
- except [:methods_excluded]
DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead
This is just depreciation warning which can turn into error if you upgrade your app to rails 5.1
, as clearly mentioned on the log you can resolve it by searching through your controller where you are using before_filter
and replace it with before_action
.
before_action
does the same thing (run some code before the request hit the action in your controller) that before_filter
does in earlier versions of rails.
Related Topics
Rails Update_Attributes Without Save
Failed to Build Gem Native Extension (Installing Compass)
How to Convert a String or Integer to Binary in Ruby
What Does the Question Mark At the End of a Method Name Mean in Ruby
How to Seed a Database in Rails
How to Find Out the Current Route in Rails
Rails 4: Assets Not Loading in Production
Natural Language Processing in Ruby
When Do Ruby Instance Variables Get Set
In Ruby How to Overload the Initialize Constructor
Accessing Elements of Nested Hashes in Ruby
What Do 'I' and '-I' in Regex Mean
How to Keep the Delimiters When Splitting a Ruby String