Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
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.