How to Use before_action in Rails Controllers
In Rails,
before_action is a controller callback that runs specified methods before certain controller actions. You use it by declaring before_action :method_name inside your controller to execute code like authentication or setup before actions run.Syntax
The before_action method is used inside a Rails controller to specify methods that should run before controller actions. You can limit which actions it applies to using only or except options.
before_action :method_name- runsmethod_namebefore every action.before_action :method_name, only: [:action1, :action2]- runs only before listed actions.before_action :method_name, except: [:action3]- runs before all except listed actions.
ruby
class ArticlesController < ApplicationController before_action :set_article, only: [:show, :edit, :update, :destroy] def show # action code end private def set_article @article = Article.find(params[:id]) end end
Example
This example shows a before_action that loads an article before the show and edit actions. It helps avoid repeating the same code in each action.
ruby
class ArticlesController < ApplicationController before_action :find_article, only: [:show, :edit] def show render plain: "Showing article: #{@article.title}" end def edit render plain: "Editing article: #{@article.title}" end private def find_article @article = Article.find(params[:id]) end end
Output
Showing article: Example Title (when accessing show with id param)
Common Pitfalls
Common mistakes when using before_action include:
- Forgetting to make the callback method
private, exposing it as an action. - Not specifying
onlyorexceptwhen needed, causing callbacks to run unnecessarily. - Using
before_filterwhich is deprecated in Rails 5 and later.
ruby
class UsersController < ApplicationController # Wrong: method is public and can be called as action before_action :authenticate_user def authenticate_user # authentication code end end # Correct way: class UsersController < ApplicationController before_action :authenticate_user private def authenticate_user # authentication code end end
Quick Reference
Use before_action to run methods before controller actions for setup or checks. Limit callbacks with only or except. Always keep callback methods private to avoid exposing them as actions.
| Option | Description |
|---|---|
| before_action :method_name | Runs method before all actions |
| before_action :method_name, only: [:a, :b] | Runs method only before listed actions |
| before_action :method_name, except: [:c] | Runs method before all except listed actions |
| Make callback methods private | Prevents them from being accessible as actions |
Key Takeaways
Use before_action to run methods before controller actions for cleaner code.
Limit callbacks with only or except to avoid unnecessary runs.
Keep callback methods private to prevent them from being called as actions.
Avoid deprecated before_filter; use before_action instead.
before_action helps keep controllers DRY by sharing common setup code.