Search code examples
controllerinstance-variables

Rails 5: Local variables in form partial


Why are some instance variables available in the view without the '@' symbol? Is it a convenience provided by Rails where instance variables matching the name of the controller are available or is something else at work here?

Ex:

picks_controller.rb

@pick = Pick.new
@show = Show.new

_form.html.erb

<%= pick.class %> // no exception raised
<%= show.class %> // "undefined local variable or method `show'" exception raised

Solution

  • This is a change in Rails 5. The default view file now passes a local variable to the form partial.

    Instead of this...

    new.html.erb

    <%= render 'form' %>
    

    This is the new default...

    new.html.erb

    <%= render 'form', person: @person %>
    

    See this pull request for more info.