思考狮身人面像显示选项

发布于 2024-10-13 01:08:31 字数 620 浏览 3 评论 0原文

好的,我有一个图形模型,并且我使用思考狮身人面像作为搜索工具。它工作得很好,但我想在搜索结果页面上显示不同的模型。例如,

我在我的图形模型中有这个,

define_index do
 indexes :name, :description, :scale, 
 indexes sub_category.name, :as => :subcategory_name
 indexes sub_category.category.name, :as => :category_name
 indexes colors.name, :as => :color_name
end

这很好,但问题是我想显示找到的搜索的所有类别和子类别,而不是只是相关的图形。在我的控制器中,我应该有三个这样的发现,

@graphics = Graphic.search params[:search]
@categories = Categories.search params[:search]
@sub_categories = SubCategories.search params[:search]

这似乎有点矫枉过正...有没有更好的方法,以便在视图中我可以单独显示它们

Ok so i have a graphic model and I am using thinking sphinx as the search tool. It works well but i want to display different models on the search results page.. for example

i have this in my Graphic model

define_index do
 indexes :name, :description, :scale, 
 indexes sub_category.name, :as => :subcategory_name
 indexes sub_category.category.name, :as => :category_name
 indexes colors.name, :as => :color_name
end

This is fine and good but the problem is i want to display all the categories and subcategories for a found search and not just the graphics that are related. In my controller should i have three find like

@graphics = Graphic.search params[:search]
@categories = Categories.search params[:search]
@sub_categories = SubCategories.search params[:search]

this seems like overkill...is there a better way so in the view i can show each of them seperately

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

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

发布评论

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

评论(2

南七夏 2024-10-20 01:08:31

您还需要在类别和子类别模型中定义索引,然后您可以同时搜索所有三个模型:

@results = ThinkingSphinx.search params[:search], :page => params[:page]

在您看来,您需要围绕每个搜索结果使用一些逻辑来呈现正确的 HTML - 也许您每个班级可以有不同的部分吗?我还建议将其包装到一个助手中。这是一个开始:

<ul>
  <% @results.each do |result| %>
    <li><%= render :partial => partial_for_search_result(result),
              :locals => {:result => result} %></li>
  <% end %>
</ul>

帮助者:

def partial_for_search_result(result)
  case result
  when Graphic
    'graphics/search_result'
  when Category
    'categories/search_result'
  when SubCategory
    'sub_categories/search_result'
  else
    raise "Unknown search result/partial mapping for #{result.class}"
  end
end

希望这能为您提供一些如何解决问题的想法。

You'll need to have indexes defined in your Category and SubCategory models as well, and then you can search across all three at once:

@results = ThinkingSphinx.search params[:search], :page => params[:page]

In your view, you'll want some logic around each search result to render the correct HTML - perhaps you can have different partials for each class? I'd also recommend wrapping it into a helper. Here's a start:

<ul>
  <% @results.each do |result| %>
    <li><%= render :partial => partial_for_search_result(result),
              :locals => {:result => result} %></li>
  <% end %>
</ul>

And the helper:

def partial_for_search_result(result)
  case result
  when Graphic
    'graphics/search_result'
  when Category
    'categories/search_result'
  when SubCategory
    'sub_categories/search_result'
  else
    raise "Unknown search result/partial mapping for #{result.class}"
  end
end

Hopefully this gives you some ideas on how to approach the problem.

聊慰 2024-10-20 01:08:31

只是为了缩短示例,您可以这样做:

控制器中

@results = ThinkingSphinx.search params[:search], :page => params[:page]

在视图中的

= render @results

应该调用每个模型部分“graphic/_graphic.html.erb”、“categories/_category.html.erb”等

Just to shorten example you can do:

in controller

@results = ThinkingSphinx.search params[:search], :page => params[:page]

in view

= render @results

should call every model partial 'graphic/_graphic.html.erb', 'categories/_category.html.erb' and so on

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