Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[ci skip] Fix explanation of controller callbacks with blocks#58229

Open
flavio-b wants to merge 1 commit into
rails:mainrails/rails:mainfrom
flavio-b:patch-2flavio-b/rails:patch-2Copy head branch name to clipboard
Open

[ci skip] Fix explanation of controller callbacks with blocks#58229
flavio-b wants to merge 1 commit into
rails:mainrails/rails:mainfrom
flavio-b:patch-2flavio-b/rails:patch-2Copy head branch name to clipboard

Conversation

@flavio-b

@flavio-b flavio-b commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Motivation / Background

The current Guides say that "action callback does not run in the scope of the controller", when you pass a block to the callback.

However, the block actually does run in the scope of the controller. You can test it with:

before_action do |controller|
  puts self.equal?(controller) # => `true`
end

Running the callback block in the context of the instance is the behaviour you get with model callbacks, so I thought it was odd that controller callbacks would work differently.

The AI did some digging, and it seems like the limitation of relying on a controller argument dates back to Rails 2.3. Then Rails 3 changed it to defining a method on the controller. Rails 4.1 changed it to use instance_exec, which is used to this day.

Detail

Removed the outdated information and tried to keep the intention and examples about the same.

Clarify that blocks passed to controller callbacks runs in the context of the controller instance.
@github-actions github-actions Bot added the docs label Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Morty Proxy This is a proxified and sanitized view of the page, visit original site.