嗨我想使用Ruby on Rails在我的电子商务网站上设置类别,我想要做的是有页面显示来自特定类别的产品,例如我有类别的T恤,所以所有的T恤都显示等...在后端我可以设置一个产品类别,因为我已经分配一个外键category_id到产品表和该类型的模型中的关系它有很多产品,而产品模型属于一个类别。为了显示特定类别的产品,我需要做些什么?在轨道上的红宝石的具体类别的产品页面
这里是我当前的代码:
存储控制器
class StoreController < ApplicationController
skip_before_filter :authorize
def index
@cart = current_cart
@products = Product = Product.search(params[:search]).paginate(:page => params[:page], :per_page => 6)
end
end
类别控制器
class CategoriesController < ApplicationController
# GET /categories
# GET /categories.json
def index
@categories = Category.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @categories }
end
end
# GET /categories/1
# GET /categories/1.json
def show
@category = Category.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @category }
end
end
end
有一些非常奇怪的东西发生在您的代码格式中,特别是在'StoreController#index'中。 – rfunduk 2012-03-26 11:56:56
奥普抱歉,我已编辑它。 – 2012-03-26 12:33:39
您的代码仍然被破坏。 'respond_to'不是一个类方法,'@ product'在任何情况下都不会是任何东西。注意你有3个'end',但只有2个代码块深?看起来你错过了'respond_to'部分的方法。 – rfunduk 2012-03-26 13:12:29