Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources Routes to Update Actions An Update Action

Why Are Params Automatically Available in Private Methods?

In Rails, you can define a private method within a controller, typically a post_params method. However, there is no need to pass the argument of params when calling this method in one of the controller methods, for example in create. Why is this and how is Rails achieving this?

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Would appreciate if someone could double-check me on this, but I believe params is actually defined as an instance method on the ActionController::Base class that all controllers inherit from. When Rails handles a request, it creates a new instance of your controller, and sets an instance variable that params simply returns. That instance variable (and therefore also the return value of params) is the same for the lifetime of that controller instance. And since params is an instance method, it can be accessed from within any instance method (public or private) of that controller.