Reputation: 1579
My shopping site has a header that's common across all pages. I have added a shopping cart icon in the header, with a number that updates based on the contents in the current cart.
Because this icon requires a definition for current_cart, I now need to add this to every controller action. Is this unwise/unsafe? I'm new to rails, and don't quite understand the security repercussions of adding methods to applications.rb
Also, is the best way to do this to add this once to application_controller.rb, or to add it separately to each relevant controller action?
Thanks in advance for the feedback!
Upvotes: 1
Views: 104
Reputation: 944
Having DRY in mind it's definitely better to add this method once, one level up in yours controller inheritance tree. If all controllers need that method then application_controller.rb is a good place. If just some of them then you may consider to create controller which inherits from ApplicationController, store that method in it, and all controllers that requiers that method should inherits form it.
Upvotes: 1
Reputation: 5791
All of your controllers inherit from the application controller. This means that any method you define in the application controller is available in all of your other controllers.
Upvotes: 0