Search code examples
ruby-on-railsrubyruby-on-rails-3partials

Do partial variables override helper methods?


I have some helper method that called current_language and sometimes I send current_language in the local_assigns.

So I want to assign my partial variable by the local_assigns's current_language in case if it's sent.

But I found something weird, in the following code:

<%
  binding.pry
  x = 4

  current_language = local_assigns[:current_language] || current_language
%>

In line 3 when debugging current_language equals nil even before overriding.

I expect it shall still equal the helper method until it's overrided.

So what is going on?


Solution

  • It’s well-documented feature of the language. The variable is kinda hoisted. Use this code to explicitly tell the parser where is the variable and where is the method call:

    #                                                                      ⇓⇓
    current_language = local_assigns[:current_language] || current_language()