2016-06-12 1 views
1

2 중첩 된 양식이있는 양식이 있습니다. 첫 번째 양식이 두 번째 양식에 삽입되는 개체를 만드는 데 사용되면 두 번째 중첩 된 양식을 새로 고치려고합니다.레일 4 jQuery/Ajax를 사용하여 새로 고치고 부분을 새로 고치고 항목을 추가 한 후

My ajax가 내 첫 f.fields_for : order_item에서 만든 order_item을 만들고 저장할 수 있도록 노력하고 있지만 다른 컨트롤러에있는 새로 고치기 부분을 가져 오는 방법을 놓치고 있습니다.

여기 여기 내 주요 형태로 내 테이블에 행으로 표시됩니다 내 중첩 된 형태로 내 부모 폼

<div class-"col-md-12"> 
<%= bootstrap_form_for(@quote) do |f| %> 
<% if @quote.errors.any? %> 
<div id="error_explanation"> 
    <h2><%= pluralize(@quote.errors.count, "error") %> prohibited this quote from being saved:</h2> 

    <ul> 
     <% @quote.errors.full_messages.each do |message| %> 
     <li><%= message %></li> 
     <% end %> 
    </ul> 
</div> 
<% end %> 
<%= f.hidden_field :quote_num, value:@current_user.company.starting_quote %> 
<fieldset> 
    <div class-"col-md-12"> 
     <div class="col-md-8"> 
      <%= f.collection_select :client_id, Client.order(:business_name), :id, :business_name, include_blank: true %> 
     </div> 
     <div class="col-md-4"> 
      <%= f.date_select :quote_exp, :order => [:month, :day, :year] %> 
     </div> 
     <div class="margin-bottom-10"></div> 
     <div class="col-md-6"> 
      <%= f.grouped_collection_select :billing_address, Client.order(:business_name), :addresses, :business_name, :full_address, :full_address, include_blank: true %> 
     </div> 
     <div class="col-md-6"> 
      <%= f.grouped_collection_select :shipping_address, Client.order(:business_name), :addresses, :business_name, :full_address, :full_address, include_blank: true %> 
     </div> 
    </div> 
</fieldset> 
<div class="margin-bottom-10"></div> 
<hr /> 
<div class="margin-bottom-10"></div> 

<!-- Render Select Options for New Item --> 
<div class="col-md-12"> 
    <%= f.fields_for :order_item do |ff| %> 
    <div class="col-md-4"> 
     <%= ff.hidden_field :quote_id, value: @quote.id %> 
     <%= ff.collection_select :category_id, Category.order(:name), :id, :name, include_blank: "Select Category", hide_label: true %> 
    </div> 
    <div class="col-md-6"> 
     <%= ff.grouped_collection_select :product_id, Category.order(:name), :products, :name, :id, :name, include_blank: true, hide_label: true %> 
    </div> 
    <div class="col-md-2"> 
     <%= ff.submit "Add Item", class:"btn-u btn-u-blue pull-right", id:"add_item" %> 
    </div> 
    <% end %> 
</div> 

<div class="margin-bottom-10"></div> 
<hr /> 
<div class="margin-bottom-10"></div> 
<div class="table-search-v1 margin-bottom-20"> 
    <div class="table-responsive"> 
     <table class="table table-hover table-bordered table-striped table-condensed"> 
      <thead> 
       <th style="width: 20px;"></th> 
       <th style="width: 300px;">Product</th> 
       <th>Price</th> 
       <th style="width: 20px;">Quantity</th> 
       <th>Discounts</th> 
       <th>Total</th> 
      </thead> 
      <tbody id="list"> 
       <!-- this is where we display the results --> 
       <%= f.fields_for :order_items do |builder| %> 
        <%= render 'order_item_fields', f: builder, show_total: true %> 
       <% end %> 

      </tbody> 
     </table> 

    </div> 
</div> 
</div> 
<div class="margin-bottom-10"></div> 
<hr /> 
<div class="margin-bottom-10"></div> 
<fieldset> 

    <div class="col-md-12"> 
     <div class="col-md-6"> 
      <%= f.text_area :special_note, rows:7, label:"Special Notes" %> 
     </div> 
     <div class="col-md-6"> 
      <div id="total_deposit"> 
      </div> 
      <div class="col-md-4"> 
       <h5>Sales Tax</h5> 
      </div> 
      <div class="col-md-8"> 
       <%= f.text_field :tax_amount, hide_label: true %> 
      </div> 
      <div class="col-md-4"> 
       <h5>Sub-Total</h5> 
      </div> 
      <div class="col-md-8"> 
       <%= f.text_field :sub_total, hide_label: true %> 
      </div> 
      <div class="col-md-4"> 
       <h5>Shipping</h5> 
      </div> 
      <div class="col-md-8"> 
       <%= f.text_field :shipping_cost, hide_label: true %> 
      </div> 
      <div class="col-md-4"> 
       <h5>Total</h5> 
      </div> 
      <div class="col-md-8"> 
       <%= f.text_field :total, hide_label: true %> 
      </div> 
     </div> 
    </div> 
</fieldset> 
<div class="margin-bottom-10"></div> 
<hr /> 
<div class="margin-bottom-10"></div> 
<div class="tag-box tag-box-v1 box-shadow shadow-effect-1 col-md-12"> 
    <h4>Deposit</h4> 
    <%= f.check_box :deposit, label: "Require Deposit" %> 
    <%= f.text_field :deposit_amount, hide_label: true, placeholder:"Enter Amount" %> 
</div> 
<div class="margin-bottom-10"></div> 
<div class="tag-box tag-box-v1 box-shadow shadow-effect-1 col-md-12"> 
    <h4>Shipping Information</h4> 
    <div class="col-md-6"> 
     <select class="form-control" id="shipping"> 
      <option value="0" selected="selected">Not Applicable</option> 
      <option>Overnight Shipping</option> 
      <option>2 Day Shipping</option> 
      <option>3 Day Shipping</option> 
      <option>Ground (5-7 days)</option> 
     </select> 
    </div> 
    <div class="col-md-6"> 
     <%= f.text_field :shipping_cost, hide_label: true, placeholder:"Enter Amount" %> 
    </div> 
</div> 
<div class="actions"> 
    <%= f.submit "Save Quote", class:"btn-u pull-right" %> 
</div> 
<% end %> 
</div> 
<script> 
$("#quote_deposit").change(function(){ 
    $("#quote_deposit_amount").toggle($(this).is(':checked')); 
}); 

$('#quote_deposit').trigger('change'); 

var product = $("#quote_product_id").val() 

</script> 

입니다. 이것은 인용 부호와 관련된 모든 항목을 표시하기 위해 작성한 후 새로 고쳐야하는 부분입니다. 가능한 경우 전체 부분을 새로 고치지 않도록 js를 사용하여 요소를 추가하고 싶습니다.

<fieldset> 
<tr> 
    <td> 
     <div class="btn-group"> 
      <button type="button" class="btn-u dropdown-toggle" data-toggle="dropdown"> 
       <i class="fa fa-angle-down"></i> 
      </button> 
      <ul class="dropdown-menu" role="menu"> 
       <li><%= f.hidden_field :_destroy %> 
       <%= link_to "Remove Item", '#', class: "remove_fields btn btn-link" %></li> 
       <li><button class="btn btn-link">Add Custom Item</button></li> 
       <li><button class="btn btn-link">Remove Item</button></li> 
      </ul> 
     </div> 
    </td> 
    <td> 
     <h5><%= f.object.product.name %></h5> 
    </td> 
    <td> 
     <h5 class="text-right"><span style="color: green"> <%= number_to_currency f.object.unit_price %></span></h5> 
    </td> 
    <td> 
     <%= f.number_field :quantity, min: 1, hide_label: true %> 
     <%= f.hidden_field :_destroy %> 
    </td> 
    <td> 
     <%= f.text_field :discount, hide_label: true %> 
    </td> 
    <td> 
     <h5 class="text-right"><span style="color: green"><%= number_to_currency f.object.total_price %></span></h5> 
    </td> 
</tr> 
</fieldset> 

내 JQuery와 아약스 호출 견적에서 적절한 경로를 생성 한 후

$('form').on 'click', '#add_item', (e) -> 
e.preventDefault() 
product = $('#quote_order_item_product_id :selected').val() 
quote = $('#quote_order_item_quote_id').val() 
console.log(product) 
console.log(quote) 
$.ajax '/order_items', 
    type: 'POST' 
    dataType: 'json' 
    data: { order_item: { product_id: product, quote_id: quote } } 
    success:(data) -> 
    alert data.id 
    error: (jqXHR, textStatus, errorThrown) -> 
    alert textStatus 

그리고 내 주문 항목 생성 방법

def create 
@order_item = OrderItem.new(order_item_params) 
respond_to do |format| 
    if @order_item.save 
    format.js do 
     render 'quotes/' + @quote.id + '/refresh' 
    end 
    else 
    format.json { render json: @order_item.errors, status: :unprocessable_entity } 
    end 
    end 
end 

답변

0

개체를 만들려면 다음 ID를/order_item 그때 내 컨트롤러를 편집 성공하면 js (또는 json은 중요하지 않습니다) 만 반환합니다. 그런 다음 jquery coffeescript 메소드를 이와 같이 변경했습니다.

$('form').on 'click', '#add_item', (e) -> 
e.preventDefault() 
product = $('#quote_order_item_product_id :selected').val() 
product_name = $('#quote_order_item_product_id :selected').text() 
console.log(product_name) 
count = $('.order-item').length 
console.log(count) 
quote = $('#quote_order_item_quote_id').val() 
console.log(product) 
console.log(quote) 
$.ajax '/order_items', 
    type: 'POST' 
    dataType: 'json' 
    data: { order_item: { product_id: product, quote_id: quote } } 
    success:(data) -> 
    id = data.id.toString() 
    $root = $('.order-item:last').clone() 

    $root.find(".remove_fields").attr('name',"quote[order_items_attributes][#{count}][_destroy]") 
    $root.find(".remove_fields").attr('id',"quote_order_items_attributes_#{count}__destroy]") 
    $root.find(".product").text(product_name) 
    $root.find(".product-id").attr('value', product) 
    $root.find(".price").text(data.unit_price) 
    $root.find(".quantity_input").val("1") 
    $root.find(".discount_input").val("") 
    $root.find(".quantity_input").attr('name',"quote[order_items_attributes][#{count}][quantity]") 
    $root.find(".quantity_input").attr('id',"quote_order_items_attributes_#{count}_quantity]") 
    $root.find(".discount_input").attr('name',"quote[order_items_attributes][#{count}][discount]") 
    $root.find(".discount_input").attr('id',"quote_order_items_attributes_#{count}_discount]") 

    $root.appendTo('#list') 

    error: (jqXHR, textStatus, errorThrown) -> 
    alert textStatus 

기본적으로 내가 무엇을하고 있는가하는 것이 성공에, 내가 다시 (데이터)로 내 개체를 얻을 후 필자는 위의 표 행을 복제하고 적절한 입력 값 모두와 함께 클라이언트 측에서 내 양식을 다시 빌드합니다. 당신은 javascript를 통해 그것을 구축 할 수 있습니다. 그렇지만 제 양식은 꽤 복잡하고 꽤 길기 때문에 믿거 나 말거나, 이것은 짧은 길이었습니다.

관련 문제