-1

在 products_controller.rb

  # GET /search/'brand'
  def brand
    @product = Product.find_all_by_brand(params[:brand])

    respond_to do |format|
      format.html # brand.html.erb
    end
  end

在路线.rb

match '/search/:brand' => 'products#brand'

如果我尝试访问localhost:3000/search/Apple,我会收到以下错误Couldn't find Product with id=Apple

有什么我想念的吗?还有其他我应该处理的文件吗?

更新

现在我得到了undefined method 'size' for nil:NilClass,我什至不确定我改变了什么。

rails 执行的查询是select "products".* FROM "products" WHERE "products"."brand" = 'Apple' ORDER BY last_seen DESC并且它们似乎返回了正确的产品。

应用程序跟踪

app/views/products/_product.html.erb:1:in _app_views_products__product_html_erb___2255278_29707176' app/views/products/brand.html.erb:1:in _app_views_products_brand_html_erb___464952485_38589588' app/controllers/products_controller.rb:52:in `brand'

4

2 回答 2

1

控制器中的注释表明您可能想尝试 /search/brand/Apple。

于 2013-03-06T01:03:35.533 回答
0

发现错误,erb 页面正在接收product而不是products. 输入错误。

于 2013-03-06T21:01:27.713 回答