Ruby on Rails中的动态选择标记

时间:2016-11-13 14:19:09

标签: ruby-on-rails ajax

我是Ruby on Rails的新手。我在一个月前开始在RoR工作。我的问题是:

如何通过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 %>

如果需要,我会提供更多信息。提前谢谢。

1 个答案:

答案 0 :(得分:0)