Swaps Swaps - 1 month ago 5
Ruby Question

How does Rails View Helper work

I recently came across a tricky situation related to rails view helpers.
The situation is like follows-

I am having a controller as

Feature1::Feature1.1::Feature1.1.1Controller
.
The
Feature1.1
also includes other controllers like
Feature1.1.2Controller
,
Feature1.1.3Controller
...

So ofcourse related view helpers in folder
app/helpers/feature1/feature1.1/...


Now the real problem I am facing is that a few helpers for
feature1.1
includes the same method name
method1
with related definition.

I was wondering how rails identifies all these helpers as I am noticing that the
method1
i.e. being called in a view for the controller
feature1.1.1
is using the definition of the
method1
i.e. written for the controller
feature1.1.2
.

So does rails consider all helper modules defined in one folder as one?

In a view
feature1/feature1.1/feature1.1.1/index
I am making a method call for
method1
.

I am using
rails3

Answer

I did some research and would like to share some additional info. As per @slowjack2k mentioned, view helpers are included by rails as a default behavior. But my question was about the situation of same method names across multiple helpers. I found this article to be useful in this scenario. Though it explains the behavior for Rails 4 but I found it behaves in the same fashion for Rails 3.2.2.

I will summarize the article -

If there will be any conflict in the same names of methods in different helper modules, rails will use method from latter file (alphabetically)