Wednesday, 3 September 2014

MONGOID SEARCH

1, add gem to your gemfile
gem 'mongoid_search'

2, bundle install
3, include below statements to ur model event.rb
include Mongoid::Search
search_in :title,:venue,:launch_date,:tags,:description,:event_type
3, create 2 routes one for post and one for get

routes.rb
get 'search/:q' => 'events_user#search_result'
post 'search' => 'events_user#search_query'

4,create below methods in your events_user controller
def search_query
if search_params[:q].empty?
redirect_to :back
else
redirect_to '/events/search/' + search_params[:q]
end
end
def search_result
@search_result = Events::Event.full_text_search(params[:q]).paginate(:page => params[:page], :per_page => 10)
@latest=Events::Event.desc(:created_at).limit(10)
@q = search_params[:q]
end
private
def search_params
params.permit(:q)
end
5, write related view code in u r search_result .html.erb

<% @search_result.each do |event| %>
<div class="details-innercontent">
<div class="media">
<%= link_to(image_tag("#{event.banner_image.url}",:class=>"img-responsive" ,:alt=>"banner"),events_details_path(:id=>event.id),:class=>"pull-left") %>
<div class="media-body">
<div class="top-content">
<%= link_to raw(event.title),events_details_path(:id=>event.id), :class=>"media-heading" %>
<p><%=h truncate(event.description, :length => 80) %></p>
<% @tags=event.tags.split(",") %>
<% for tag in 0...@tags.length %>
<%= link_to @tags[tag], "#", :class=>"drama" %>
<% end %>
</div>
<div class="bottom-content">
<span class="pull-left"><%= event.created_at.strftime("%d %b %y") %></span>

</div> </div> </div> </div> <% end %>

Enjoy...............:-)

No comments:

Post a Comment