gilsilas
gilsilas

Reputation: 1461

rails helpers location

I made a simple helper with one function and put it in the relevant controller_helper and I noticed that the function is valid from the whole application views.

Why is that? shouldn't the helper be valid only to his controller?

Upvotes: 1

Views: 314

Answers (2)

Peter Cumberland
Peter Cumberland

Reputation: 1

That would make sense, but it's not the complete story. I have a controller MainController and a controller AlsoController, and defined a helper with the same name wherefrom in each of the helper modules ie MainHelper and AlsoHelper. But if I try to make use of wherefrom in the also view, it uses the helper defined in MainHelper instead of the one in AlsoHelper.

Upvotes: 0

apneadiving
apneadiving

Reputation: 115531

The process is the following:

If you're in a view belonging to controller foo and you call a helper called my_helper:

  • if defined in foo's helper, it's executed from here

  • if not defined in foo's helper but defined in another helper, say bar, it's executed from there

  • if not in foo nor in any bar, it's checked in application_helper. If it doesn't exist here, an error is raised.

Upvotes: 2

Related Questions