填充Cocoon +自动完成 - 错误:ActiveRecord :: RecordNotFound

时间:2015-12-11 19:22:53

标签: ruby-on-rails activerecord autocomplete cocoon-gem

我使用这些宝石,我想根据配料名称填写表格。一切正常。但是,当我尝试创建配方时,我收到以下错误:
对于ID = 的MealIngredient,找不到ID = 1的成分

<小时/> 视图:
这是Cocoon

所需的 _form 部分
console.log($('section#members'));

此_form呈现以下 _meal_ingredient_fields 部分

<div class = "row">
  <div class= "col-md-10 col-md-offset-1">
    <%= simple_form_for(@meal) do |f| %>
      <%= f.error_notification %>
      <div class="form-inputs">
        <%= f.input :name %>
      </div>
      <h3>ingredientes</h3>
      <fieldset id="ingredients">
        <ol>
          <%= f.fields_for :meal_ingredients do |meal_ingredient| %>
            <%= render "meal_ingredient_fields", :f => meal_ingredient  %>
          <% end %>
        </ol>

        <div class="form-actions">
          <div><%= link_to_add_association 'adicionar ingrediente', f, :meal_ingredients, 'data-association-insertion-node' => "#ingredients ol", 'data-association-insertion-method' => "append", :wrap_object => Proc.new {|quantity| quantity.build_ingredient; quantity }, :class => "btn btn-default" %></div>
        </div>
      </fieldset>
      <div class="form-actions">
        <div><%= f.button :submit, class: "btn btn-success" %></div>
      </div>
    <% end %>
  </div>
</div>

<小时/> 的 JAVASCRIPT:
ingredients.js

<div class = "nested-fields">
  <table class= "table">
      <thead>
        *
        *
        *
      </thead>
      <tbody>
        <tr>
          <%= f.fields_for :ingredient do |fi| %>
            <%= fi.hidden_field :id %>
          <td scope="row" class="col-md-4">
            <%= fi.autocomplete_field :name, autocomplete_ingredient_name_ingredients_path, :id_element => '#ingredient_id', class: "form-control" %>
          </td>
          <td class="col-md-1">
            <%= fi.text_field :unit, required: true, class: "form-control unit_input" %>
          </td>
          <td class="col-md-1">
            <%= fi.text_field :carb, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control carb_input" %>
          </td>
          <td class="col-md-1">
            <%= fi.text_field :prot, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control prot_input" %>
          </td>
          <td class="col-md-1">
            <%= fi.text_field :fat, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control fat_input" %>
          </td>
          <% end %>
          <td class="col-md-1">
            <%= f.number_field :quantity, required: true, :pattern => '^\d+(\.\d+)*$', title: "Apenas números separados por pontos", class: "form-control" %>

          </td>          
          <td class="col-md-1">
          </td>
          <td class="col-md-1">
            <%= link_to_remove_association "remove item", f, :class => "btn btn-danger" %>
          </td>
        </tr>
      </tbody>
  </table>
</div>

如果需要其他内容,这是我的回购:https://github.com/betinhosaad/inutree
谢谢!

EDITED
PARAMS:

 $(function(){
  var num;
  var id;
  $('#ingredients').on('cocoon:after-insert', function(e, insertedItem) {
    $('input[id^="meal_meal_ingredients_attributes_"][id$="_ingredient_attributes_name"]').each(function(){
        var current = $(this);
        var rx = /meal_meal_ingredients_attributes_(.*)_ingredient_attributes_name/;
        num = rx.exec(current.attr('id'))[1];
        id = '#meal_meal_ingredients_attributes_'+ num +'_ingredient_attributes_';
    });
    $(id + 'name').bind('railsAutocomplete.select', function(event, data){
      $( id + 'id' ).val( data.item.id);
      $( id + 'unit' ).val( data.item.unit).prop('disabled', true);
      $( id + 'carb' ).val( data.item.carb).prop('disabled', true);
      $( id + 'prot' ).val( data.item.prot).prop('disabled', true);
      $( id + 'fat' ).val( data.item.fat).prop('disabled', true);
    });
  });
}); 

0 个答案:

没有答案