开发者

rails helpers location

开发者 https://www.devze.com 2023-03-03 14:53 出处:网络
I ma开发者_运维问答de 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.

I ma开发者_运维问答de 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消