All threads / Access local variable in partial

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Access local variable in partial

Javier Bonet asked in Rails
Hi there,

I'm working on a budgeting section of a graphic designer web and I created the following model hierarchy:
general_budget.rb
class GeneralBudget < ActiveRecord::Base
  has_many :budgets

  accepts_nested_attributes_for :budgets, reject_if: :all_blank, allow_destroy: true
end

budget.rb
class Budget < ActiveRecord::Base
  belongs_to :sheet_type
  belongs_to :general_budget
  ...
end

sheet_type.rb (A4 photography 120 grs.)
class SheetType < ActiveRecord::Base
  belongs_to :sheet_size
end

sheet_size.rb (A5, A4, ...)
class SheetSize < ActiveRecord::Base
  has_many :sheet_types
end

And I'm using cocoon gem to create multiple budgets while creating a general budget.

I finished with the section where the designer budget, the problem come up when he wants to edit an specific budget. new.html.erb and edit.html.erb are pretty simple

general_budgets/new.html.erb
<link href="https://fonts.googleapis.com/css?family=Roboto+Slab:300,400,700" rel="stylesheet">

<div>

  <%= image_tag "logo.gif", class: "logo" %>

  <%= render 'form' %>

  <%= link_to 'Atras', url_for(:back) %>

</div>

general_budgets/edit.html.erb
<h1>Editando Presupuesto General</h1>

<%= render 'form' %>

<%= link_to 'Mostrar', @general_budget %> |
<%= link_to 'Atras', url_for(:back) %>

general_budgets/_form.html.erb
<%= form_for(@general_budget) do |f| %>
  <% if @general_budget.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@general_budget.errors.count, "error") %> prohibited this general_budget from being saved:</h2>

      <ul>
      <% @general_budget.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="budgets">
    
   <%= f.fields_for :budgets do |budget| %>
      <%= render 'budget_fields', { f: budget, size: budget.object.sheet_type.sheet_size.id } %>
    <% end %>


    <div class="links agregar_presupuesto">
      <%= link_to_add_association f, :budgets do%>
        <i class=" icon-plus-circled"> Agregar Presu </i>
      <% end %>
    </div>
  </div>

  <div id="general_data">
    
    <!-- GENERAL BUDGET INPUT FIELDS -->
    
    <div class="actions crearPresu">
      <%= f.submit 'Finalizar Presu' %>
    </div>
  </div>
<% end %>

general_budgets/_budget_fields.html.erb
<div class="nested-fields miniBudget">
  
  
  ...
    
  <div class="papel">

    <%= local_assigns[:size] %> <----------- Here the value shows up with no problem

    <div class="field papelSize">
      <%= f.select :sheet_type_id, SheetSize.find(local_assigns[:size]).sheet_types.collect { |p| [ p.full_name, p.id ] }, { prompt: "Tipo papel" }, { class: 'sheet_type' }%>
                                                         ^
                                                         |
                                       Here seems to have nil value
    </div>

  </div>

  ...


</div>

I get the possible sheet types this way ( SheetSize.find(local_assigns[:size]).sheet_types ) because I need to show sheet types related to the the designer have chosen previously (i.e. If he chose A4, I want sheet type select display A4 sheet types)

The error in the browser is
Couldn't find SheetSize with 'id'= 

 Extracted source (around line #43): 
 | 41  
 | 42  <div class="field papelSize">
 | 43    <%= f.select :sheet_type_id, SheetSize.find(local_assigns[:size].to_s).sheet_types.collect { |p| [ p.full_name, p.id ] }, { prompt: "Tipo papel" }, { class: 'sheet_type' }%>
 | 44  </div>
 | 45
 | 46
       <div class="field">

I've been googling for a while and can't get any clue.

Any idea what's going on?

Thanks.
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 35,699+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.