Aarthi
Aarthi

Reputation: 1521

Helper directory in rails

Will a file in helper directory included in all controllers?. I didn't find any good explanation regarding this. I have 2 custom directories in my controller( like admin, for normal user). Do I have same directory structure at my helper?. Is Helper name same as controller name only for readability?

Upvotes: 5

Views: 5378

Answers (2)

Romanch Sharma
Romanch Sharma

Reputation: 132

Helper is just a ruby module which is openly available for views and controllers. You should never keep your code in helper if you do not want it to expose to views.

If you want to use helper methods for all your controller and views. Then you can add methods to application helper and include it to application controller. However if you don't want to expose methods to views, then you can use rails concerns. create a methods inside it and include it inside different controllers.

No helper do not name the same name only for readability. you still need to include inside your same name controller to call functions if you want to use it inside controller. But you can still use inside views methods with same name.

Upvotes: 4

kasperite
kasperite

Reputation: 2478

By default all helper files under app/helpers are included in all controllers. As such, it doesn't matter how you structure what's inside helpers folder. If you really want to enforce controller to only include matching helper then set config.action_controller.include_all_helpers in config to false.

See comment section for details: https://github.com/rails/rails/blob/b5db73076914e7103466bd76bec785cbcfe88875/actionpack/lib/action_controller/metal/helpers.rb

Upvotes: 5

Related Questions