具有嵌套资源的 Rails 应用程序,需要 SearchLogic 帮助

发布于 2024-09-25 14:00:42 字数 1502 浏览 3 评论 0原文

我正在尝试在嵌套资源上设置搜索逻辑。我有类别 has_many :products 也有类别 has_many :brands 到 :products

因此,从结构上讲,它的类别/品牌/产品

当用户导航网站时,他们单击一个类别,该类别使用 Category#show 操作。

#Category_controller
def show
  @category = Category.find_by_url_name(params[:id])
  @brands = @category.brands
  @categories = Category.find(:all)
  @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first


      @products = @category.products.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end

因此,它会呈现该类别的可用品牌列表,并显示第一个列出的品牌中的产品。

如果用户随后单击列表中的其他品牌,则会将用户带到 Brand#show 操作。

#Brands_controller
def show
  @category = Category.find_by_url_name(params[:category_id])
  @brand = Brand.find(params[:id])
  @search = Product.search(params[:search])
  @products = @search.paginate(:conditions => {:brand_id => @brand, :category_id => @category}, :page => params[:page])
  @meta_title = "#{@brand.name}"
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @brand }
  end

我已经开始实现 Searchlogic,安装了 gem,并准备了一些代码。但是当它搜索时,它会从 url 中删除类别 http://localhost:3000/brands/14?search[order]=ascend_by_price 虽然如果它有效的话,这不是一个大问题,但事实并非如此。我有需要访问相关类别和品牌信息的面包屑...

所以最终我试图学习如何在嵌套资源上实现 Searchlogic。谁能在这段旅程中引导我?

end

I am trying to setup searchlogic on nested resources. I have Category has_many :products also Category has_many :brands through :products

So structurally its Category/Brand/Product

As a user navigates the site they click a category, which uses the Category#show action.

#Category_controller
def show
  @category = Category.find_by_url_name(params[:id])
  @brands = @category.brands
  @categories = Category.find(:all)
  @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first


      @products = @category.products.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end

So it renders the list of available brands for that Category, and also display the products in the first listed brand.

If the user then clicks a different brand from the list, the user is taken to the Brand#show action.

#Brands_controller
def show
  @category = Category.find_by_url_name(params[:category_id])
  @brand = Brand.find(params[:id])
  @search = Product.search(params[:search])
  @products = @search.paginate(:conditions => {:brand_id => @brand, :category_id => @category}, :page => params[:page])
  @meta_title = "#{@brand.name}"
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @brand }
  end

I have started implementing Searchlogic, gem installed, and some code in place. But when it searches it removes the category from the url
http://localhost:3000/brands/14?search[order]=ascend_by_price
While its not a major problem if it worked, it doesn't. I have breadcrumbs that need to have access to the relevant category and brand info...

So at the end of the day I am trying to learn how to implement Searchlogic on nested resources. Can anyone guide me along in that journey?

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℡Ms空城旧梦 2024-10-02 14:00:42

我能够在类别#show 上运行它,但仍然不能在品牌#show 上运行。类别#show 中的修复是

`     def show
    @category = Category.find_by_url_name(params[:id])
    @brands = @category.brands
    @categories = Category.find(:all)
    @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first

      @search = @category.products.search(params[:search])
      @products = @search.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end`

品牌仍然无法正常工作,有什么想法吗?

I was able to get this running on the category#show but still not Brand#show. the fix in Category#show was

`     def show
    @category = Category.find_by_url_name(params[:id])
    @brands = @category.brands
    @categories = Category.find(:all)
    @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first

      @search = @category.products.search(params[:search])
      @products = @search.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end`

Brands still doesn't work right, any ideas?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文