title: Rails 4 - set display value when editing autocomplete nested fields_for
body: <p>I'm developing a form that links recipes, recipe_entries (has_many_though join table) and ingredients with jQuery autocomplete in Rails 4. I'm using a combination of of the simple_form, cocoon and rails4-autocomplete gems. The idea is that users can create a recipe and dynamically add and edit associated ingredients via autocomplete (the quantity for each ingredient is stored on the join table).</p>
  
  <p>I have most functionality working, however the only issue that still bugs me is that I had to create an f.input for :ingredient that when editing the recipe shows values, such as the below image and not the desired name of the associated ingredient.</p>
  
  <p><img src="https://i.stack.imgur.com/1QpK1.png" alt="enter image description here"></p>
  
  <p>Other than that, I can dynamically create, delete and update all ingredient associations. Any tips are much appreciated. Here my code:</p>
  
  <p>Gemfile</p>
  
  <pre><code>gem 'rails', '4.0.2'
  gem 'jquery-rails'
  gem 'jquery-ui-rails'
  gem 'simple_form'
  gem "cocoon"
  gem 'rails4-autocomplete'
  </code></pre>
  
  <p>Recipe.rb</p>
  
  <pre><code>class Recipe &lt; ActiveRecord::Base
      has_many :recipe_entries, :dependent =&gt; :destroy
      has_many :ingredients, through: :recipe_entries
  
      accepts_nested_attributes_for :recipe_entries,:allow_destroy =&gt; true 
      accepts_nested_attributes_for :ingredients
  end
  </code></pre>
  
  <p>RecipeEntry.rb</p>
  
  <pre><code>class RecipeEntry &lt; ActiveRecord::Base
    belongs_to :recipe
    belongs_to :ingredient
  end
  </code></pre>
  
  <p>Ingredient.rb</p>
  
  <pre><code>class Ingredient &lt; ActiveRecord::Base
      has_many :recipe_entries
      has_many :recipes, through: :recipe_entries
  end
  </code></pre>
  
  <p>Recipes form:</p>
  
  <pre><code>&lt;%= simple_form_for(@recipe) do |f| %&gt;
    &lt;% if @recipe.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@recipe.errors.count, "error") %&gt; prohibited this recipe from being saved:&lt;/h2&gt;
        &lt;ul&gt;
        &lt;% @recipe.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
      &lt;%= f.input :name %&gt;
      &lt;%= f.input :description %&gt;
      &lt;div id="recipe_entries"&gt;
        &lt;%=  f.simple_fields_for :recipe_entries, :input_html =&gt; { :class =&gt; "form_inline" } do |entry|%&gt;
            &lt;% render 'recipe_entry_fields', :f =&gt; entry %&gt;    
        &lt;% end %&gt;
        &lt;%= link_to_add_association 'add recipe entry', f, :recipe_entries%&gt; 
      &lt;/div&gt;
     &lt;%= f.button :submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>recipe_entry_partial</p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
  
      &lt;% @it=f.options[:child_index] %&gt;
      &lt;%= f.input :ingredient_id, as: :hidden, input_html: {id: "ingredient_id#{@it}"} %&gt;
      &lt;%= f.input :ingredient, :url =&gt; autocomplete_ingredient_name_recipes_path, :as =&gt; :autocomplete, :input_html =&gt; {id_element: "#ingredient_id#{@it}"}, placeholder: "Enter ingredient..." %&gt;
      &lt;%= f.input :quantity %&gt;
  
      &lt;%= link_to_remove_association "remove entry form", f %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  I believe that the easiest way of solving the edit display issue is to pre-insert the correct value in the f.input containing the autocomplete. e.g.</p>
  
  <pre><code>&lt;%= f.input :ingredient, :url =&gt; autocomplete_ingredient_name_recipes_path, :as =&gt; :autocomplete, :input_html =&gt; {id_element: "#ingredient_id#{@it}", value: f.object.ingredient.name}, placeholder: "Enter ingredient..." %&gt;
  </code></pre>
  
  <p>Unfortunate, this only works up to the recipe entries association. This means that I can still get values for e.g. f.object.ingredient_id which returns the correct result. However, I can't get the associated ingredient any more. I seem to be able to get the associated ingredient but when I try to e.g. call the .name method on it, I receive a nil error.</p>
  
  <p>Strangely, I can get the ingredient_id by calling f.object.ingredient_id and can retrieve an Ingredient from the Model by calling e.g. Ingredient.get(1).name. BUT, I cannot dynamically link the two values. Ingredient.get(f.object.ingredient_id) returns a nil error. Also with to_string or to_integer calls.</p>