Question

I have a helper with a method named search_form like this:

module Admin::BaseHelper
  def search_form(*args)
    # my great code here
  end
end

To call this method in my HAML code, I can do this:

= search_form
= search_form()
= search_form(param1: "value1", param2: "value2"...)

My problem is with this first call. When I do this in any HAML file, it renders my helper. Except if my file name is _search_form.html.haml. Is that case, it returns nil.

If I put a raise error in the helper, I notice that my method isn't being called, but I am not able to find what is being called and why.

If I use the syntax on the second and third lines, it works as expected by calling my helper method.

So my question is: is this standard Rails behavior or a bug?

Was it helpful?

Solution

By default, Rails will look for a local variable with the same name as your partial, which may conflict with existing method names.

One way to get around this is to simply redefine the method inside your partial:

<% search_form = self.search_form %>
# Rest of the partial's code
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top