Skip to main content

Access local variable in partial

Rails • Asked by Javier Bonet
Hi there,

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

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

class Budget < ActiveRecord::Base
  belongs_to :sheet_type
  belongs_to :general_budget

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

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

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

<link href=",400,700" rel="stylesheet">


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

  <%= render 'form' %>

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


<h1>Editando Presupuesto General</h1>

<%= render 'form' %>

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

<%= 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>

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

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

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

  <div id="general_data">
    <div class="actions crearPresu">
      <%= f.submit 'Finalizar Presu' %>
<% end %>

<div class="nested-fields miniBudget">
  <div class="papel">

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

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




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    <%= :sheet_type_id, SheetSize.find(local_assigns[:size].to_s).sheet_types.collect { |p| [ p.full_name, ] }, { 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?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.