【问题标题】:Dynamic select tag in Ruby on RailsRuby on Rails 中的动态选择标签
【发布时间】:2016-11-14 00:05:21
【问题描述】:

我是 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 %>

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

【问题讨论】:

    标签: ruby-on-rails ajax


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多