Search code examples
ruby-on-rails-3partial-viewserbrenderpartial

partial local only accessible using local_assigns, not exposed by name


I am passing two locals from a view ERB to a partial. Both locals are successfully passed in local_assigns. However, I am only able to use the FormBuilder via a local variable name in the partial. The other value is usable within my partial as local_assigns[:disable_edits], but not as disable_edits.

_form.html.erb

    <div>
    <%= f.fields_for :panels do |builder| %>
        <%= render "panel_fields", :f => builder, :disable_edits => true %>
    <% end %>
</div>

_panel_fields.html.erb

<div>
<p>
    <%= local_assigns[:disable_edits] %>
</p>
<p>
    <%#= disable_edits ? 'disable edits true' : 'disable edits false' %>
</p>
<p>
    <%= local_assigns.keys %>
</p>

local_assigns[:disable_edits] results in "true" being displayed.

local_assigns.keys results in "[:f, :disable_edits, :panel_fields]" being displayed.

Uncommenting the ternary statement results in "undefined local variable or method `disable_edits' for #<#:0x4d58768>"

I am following what I understand is the latest suggested Rails syntax, but have tried manipulations using :partial=>, :locals=>, :as=>, etc. to no avail. I also do not think I've made this an optional argument in any way, so a lot of the information about testing with has_key vs. nil? isn't helping me. My understanding is everything in local_assigns should be exposed as in the partial as a local variable with the same name as the hash key.

I'm getting by for now using local_assigns[:disable_edits], but would like to understand what is happening and correct things so I can use more conventional syntax. Thanks!


Solution

  • try using

    render :partial => "panel_fields", :locals => {:f => builder, :disable_edits => true }