具有嵌套资源的 Rails 应用程序,需要 SearchLogic 帮助
我正在尝试在嵌套资源上设置搜索逻辑。我有类别 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够在类别#show 上运行它,但仍然不能在品牌#show 上运行。类别#show 中的修复是
品牌仍然无法正常工作,有什么想法吗?
I was able to get this running on the category#show but still not Brand#show. the fix in Category#show was
Brands still doesn't work right, any ideas?