开发者

Sunspot Rails - Can user change what fields to search?

开发者 https://www.devze.com 2023-04-11 12:03 出处:网络
I started using Sunspot to perform searc开发者_如何学Pythonhes in my Rails 3 app and I ran into a doubt. Is there a way that I can let the user choose in what fields he/she wants to search. For exampl

I started using Sunspot to perform searc开发者_如何学Pythonhes in my Rails 3 app and I ran into a doubt. Is there a way that I can let the user choose in what fields he/she wants to search. For example, in my app we have:

class Project < ActiveRecord::Base
    searchable do
      text :name, :content, :keyword
    end
end

And in the View the default search bar:

<%= form_tag projects_path, :method => :get do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
    <%= submit_tag "Search", :name => nil %>
  </p>
<% end %> 

Can I add a radio button or something like that so the user can mark it in order just to search by name, or content or keyword? If yes, how can I make it?

Thanks a lot.


Watch the railscasts on the subject: http://railscasts.com/episodes/278-search-with-sunspot .. in it, Ryan lets user's query optionally by month.

So, if month was sent in:

def index
  @search = Article.search do
    fulltext params[:search]
    with(:publish_month, params[:month]) if params[:month].present?
  end
  @articles = @search.results
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号