7步给商品添加分类功能

Published on:

写在前面,这篇小帖子写了好久了,现在来看并不是一个好的实作分类的方法,若想更好的方法,请参考这篇实作:

http://zhengzhi1-blog.logdown.com/posts/1373934-add-a-category

说明:

  • 先考虑为产品做那几个分类,比如我会把女装简单分成5类,分别是”dresses","jeans", "skirts", "shorts", "suits”。
  • $ xxxxxx表示命令行输入命令

1.add category to migration & schema

$ rails g migration add_category_to_product category:string
打开xxxxxxxxxxxxxx_add_category_to_product.rb,应该长得像酱紫:

class AddCategoryToProduct < ActiveRecord::Migration[5.0]
  def change
    add_column :products, :category, :string
  end
end

$ rake db:migrate

2.修改views/admin/products/new.html.erb 和 edit.html.erb
增加一栏

<div class="group">
    <%= f.input :category, :collection => ["dresses","jeans", "skirts", "shorts", "suits"], :selected => "dresses" %>
  </div>

3.修改app/controllers/admin/products_controller.rb

def product_params
params.require(:product).permit(:title, :description, :quantity, :price, :image, :category)
end

4.add routes

resources :products do
    collection do
      get :dresses
      get :jeans
      get :skirts
      get :shorts
      get :suits
    end

5.在app/controllers/products_controller.rb增加分类的功能

def dresses
    @products = Product.where(:category => "dresses").paginate(:page => params[:page], :per_page => 5)
  end

  def jeans
    @products = Product.where(:category => "jeans").paginate(:page => params[:page], :per_page => 5)
  end

  def skirts
    @products = Product.where(:category => "skirts").paginate(:page => params[:page], :per_page => 5)
  end

  def shorts
    @products = Product.where(:category => "shorts").paginate(:page => params[:page], :per_page => 5)
  end

  def suits
    @products = Product.where(:category => "suits").paginate(:page => params[:page], :per_page => 5)
  end

6.增加分类页面
一共5个页面
$ touch dresses.html.erb
$ touch jeans.html.erb
$ touch skirts.html.erb
$ touch shorts.html.erb
$ touch suits.html.erb

7.增加分类的小按钮
比如我会再navbar页面加上这几个按钮

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
              <li>
                <%= link_to("Dresses", dresses_products_path) %>
              </li>
            </ul>
            <ul class="nav navbar-nav">
              <li>
                <%= link_to("Jeans", jeans_products_path) %>
              </li>
            </ul>
            <ul class="nav navbar-nav">
              <li>
                <%= link_to("Skirts", skirts_products_path) %>
              </li>
            </ul>
            <ul class="nav navbar-nav">
              <li>
                <%= link_to("Shorts", shorts_products_path) %>
              </li>
            </ul>
            <ul class="nav navbar-nav">
              <li>
                <%= link_to("Suits", suits_products_path) %>
              </li>
            </ul>
        </div>

或者也可以吧链接放在图片上:

<a href=“/products/dresses“>
                    <img
                        height="1100"
                        src="http://n.nordstrommedia.com/id/494c00d2-0811-4bb9-b6f0-bd166ff61a74.jpeg?w=2560&amp;h=1100"
                        width="2560"
            height="1100"/>
                </a>

Comments

comments powered by Disqus