2016-11-13 61 views
0

我是Ruby on Rails的新手。我在一個月前開始在RoR工作。我的問題是:Ruby on Rails中的動態選擇標記

如何製作動態選擇標籤,通過AJAX根據產品型號的選定顏色顯示尺寸?

每個產品都有很多變體。每種變體都有一種顏色,一種尺寸和數量。

這裏是我的模型:

class Color < ActiveRecord::Base 
    has_many :variants, dependent: :destroy 
    validates :name, presence: true, length: {minimum: 2, maximum: 20} 
    validates :hex, presence: true, uniqueness: true, length: { is: 7}, format: { with: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/ } 
end 

class Size < ActiveRecord::Base 
    has_many :variants, dependent: :destroy 
    validates :size, uniqueness: true, numericality: { only_integer: true, greater_than_or_equal_to: 28, less_than_or_equal_to: 49} 
end 

class Variant < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :size 
    belongs_to :color 

    validates :quantity, :numericality => { :greater_than_or_equal_to => 0 } 
    validates :color_id, presence: true 
    validates :size_id, presence: true 
end 

這裏是我的產品的看法:

<%= form_tag({controller: "carts", action: "add"}, method: "post", class: "form") do %> 
    Color: 
    <%= select_tag :color, options_from_collection_for_select(@colors, "name", "name"), id: 'color', prompt: 'Select color', onchange: 'onChange()' %> 
    Quantity: 
    <%= number_field_tag :quantity, '1', min: 1%> 
    Size: 
    <%= select_tag :size, options_from_collection_for_select(@sizes,"size", "size"), id: 'size', prompt: 'Select size', onchange: 'onChange()'%> 

    <%= submit_tag "ADD TO CART", class: 'btn btn-default' %> 
    <%= submit_tag "BUY NOW", class: 'btn btn-dark-grey', style: "margin-left:10px"%> 
<% end %> 

我會提供更多的信息,如果它是需要的。提前致謝。

回答