mardi 1 décembre 2015

How to Choose Category In Form then show list of category.sizes to choose

I have 3 models products, category, and size.

In my Products new view, I can choose a category.

When i choose a category i want the category sizes to show below it as a tick box list. How do you do this?. Example: Someone might choose Jeans as the category then below will show all the sizes for jeans as a tick box

Thank you in advance for any help.

Here is my Product view

<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @product, html: { multipart: true } do |f| %>
            <%= f.input :image, label:"Choose Image"%>
            <%= f.collection_select :category_id, Category.order(:name), :id, :name, include_blank: true, :prompt => "Select One Category" %>
            <%= f.input :title, label:"Title"%>
            <%= f.input :price, label:"Price"%>
            <%= f.input :description,label:"Description" %>
            <%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
            <%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

Product model

class Product < ActiveRecord::Base
  acts_as_taggable

    belongs_to :user
    belongs_to :category

  has_many :product_sizes

    validates :title, presence: true, length: { maximum: 30 }
    validates :description, presence: true, length: { maximum: 2000 }
    validates :category, :user_id, :price, presence: true

    has_attached_file :image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

Category model

class Category < ActiveRecord::Base
  has_ancestry

  has_many :products
  has_many :sizes

  validates :name, presence: true, length: { maximum: 20 }, uniqueness: true

  accepts_nested_attributes_for :sizes, allow_destroy: true
end

Size model

class Size < ActiveRecord::Base
    validates :title, presence: true, length: { maximum: 15 }
    validates :title, uniqueness: true

  belongs_to :category
end

Aucun commentaire:

Enregistrer un commentaire