2016-08-11 4 views
0

いくつかのjavascripts/AJAXで軽微な問題が発生しました。誰かが私を助けたり、正しい方向に向けることを望んでいました。フォームにテキストボックスを設定するドロップダウンボックスの選択

私がしようとしているのは、同一フォーム内のcollection_selectを使用して、カートンごとの価格フィールドを設定することです。これは私の注文テーブルへのエントリを作成すると考えられる注文フォームです。以下のようにフォームビュー:

new.html.erb

<h1>Add a new order for <%= @customer.name %></h1> 

<div class="row"> 
    <%= form_for(@order) do |f| %> 
    <div class="col-md-6"> 
     <%= render 'shared/error_messages', object: f.object %> 
     <%= f.hidden_field :customer_id, :value => @customer.id %> 

     <h3>Order Details</h3> 
     <%= f.label :address_id, "Delivery Address" %> 
     <%= f.collection_select :address_id, @addresses, :id, :select_address, :prompt => "Select delivery location",class: 'form-control' %></br>  
     <%= f.label :remark, "Order Remark" %> 
     <%= f.text_area :remark, class: 'form-control' %></br> 

     <h3>What items would you like to place?</h3> 
     <%= f.add_nested_fields_link :single_orders, "Add Product" %> 
     <%= f.nested_fields_for :single_orders do |builder| %> 
     <%= builder.collection_select :product_id, @products, :id, :select_product, {:prompt => "choose product"}, {:class => "product_selection form-control"} %> 
     <%= builder.text_field :ctn_price, placeholder: "Price/carton", id: "price", class: 'ctn_price_field form-control' %> 
     <%= builder.text_field :qty, placeholder: "Quantity",id: "quantity", class: 'form-control' %> 
     <%= builder.text_field :price, placeholder: "Amount", id: "total-amount", readonly: true, class: 'form-control' %> 
     <%= builder.remove_nested_fields_link %> 
     <% end %> 
    </div> 
     <%= f.submit "place order", class: "btn btn-primary" %> 
    <% end %> 
</div> 

私はcollection_selectをしたいと思います:PRODUCT_IDは、製品テーブル内のすべての製品を引き出します。ユーザーがドロップダウンから注文したい商品を選択すると、選択した商品の価格がtext_field:ctn_priceに入力されます。

class SingleOrder < ActiveRecord::Base 
    belongs_to :order 
    belongs_to :product 

    validates :order,  presence: true 
    validates :product_id, presence: true 
    validates :qty,   presence: true 
    validates :price,  presence: true 
    validates :ctn_price, presence: true 
end 

single_order.rb

product.rb

class Product < ActiveRecord::Base 
    has_many :special_prices, dependent: :destroy 
    has_many :single_orders 
    has_many :package_orders_products 

    before_save :upcase_stock_code 

    validates :name,  presence: true, length: { maximum: 50 } 
    validates :stock_code, presence: true, length: { maximum: 20 } 
    validates :status,  presence: true, length: { maximum: 10 } 
    validates :price,  presence: true 

    private 
    def select_product 
     "#{name} - #{price}" 
    end 

    # Converts stock_code to all upper-case. 
    def upcase_stock_code 
     self.stock_code = stock_code.upcase 
    end 
end 

私は事前に任意の助け日:( 感謝のために立ち往生しているとしてすべてのヘルプは高く評価される。

+0

ユーザーがドロップダウンリストで製品を選択すると、製品IDを持つサーバーにajaxリクエストを呼び出してから、製品の価格を取得し、価格のテキストフィールドの値を更新できます。 – Thanh

答えて

0

ますajax呼び出しを使用してこれを行うことができます。その前にproduct_idを入力として取得し、@ productを出力としてレンダリングするapiを作成します。

$("#product_selection_id").on('change', function(){ 
    var product_id = $(this).val(); 

    $.ajax({ 
     method: "GET", 
     type: 'html', 
     url: "call_url_for_get_product_api_with_product_id", 
     cache: false, 
     success: function(data, textStatus, jqxhr) { 
     console.log("success"); 
     } 
    }); 
    }); 

url_for_get_product_apiget_productアクションにjsリクエストとして送信されるとします。ここで私はあなた自身によるパターンimplimentこれを与えている:

$('#ctn_price_field').val(@product.price) 
#ihave not tested this code change this according to your requirements. 

注意を次のようにget_product.js.erbでテンプレートを作成します。ありがとう。

+0

あなたのお返事ありがとうございます!私はあなたが上記で提供したコンセプトを試していますが、私はあなたが私を助けることができるかどうか疑問に思っていたルートの部分に固執しています..注文のルートルートは以下の通りです resources:orders do メンバーdo get 'get_product_prices 」、へ: 『注文番号のget_product_prices』 ポスト:作成:非アクティブ、アクティブ エンド コレクション行う エンド エンド が、 『get_product_prices』 URLを取得することができ、唯一取得していないようです」新しい "URL ..私が遭遇するエラーはこれです: ActiveRecord :: RecordNotFound app/controllers/orders_controller.rb:8:in 'new ' –

+0

新しいルートファイルコードで質問を更新できますか? –

関連する問題