Sunspot Solr Rails searching/sorting via JQuery Ajax?
I'm currently using the sunspot_rails gem for a simple 'craigslist' ecommerce app and I want to load search results/sorting via JQuery Ajax (rather than a page request). Howe开发者_StackOverflow中文版ver, I'm having difficulty getting this to work, any ideas? JQuery is working for other plugins. Thank you very much!
Search Action and sorting functions in Item Controller
def search
@search = Item.search do
fulltext (params[:search])
order_by sort_column, sort_direction
end
@items = @search.results
end
def sort_column
Item.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
search.js
$("#itemsfeed").html(<%= escape_javascript(render('feed')) %>);
Item Helper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
search.html.erb
<script>
$(document).ready(function() {
$("#itemsfeed th a").live("click", function() {
$.getScript(this.href);
return false;
});
});
</script>
<% form_tag search_items_path, :method => 'get' do %>
<div class='searchbox'>
<%= text_area_tag :search, params[:search], :id => 'tags', :cols => '40', :rows => '1' %>
</div>
<%= submit_tag "Search", :name => nil, :class => 'submitbtn' %>
<br/>
<div id='itemsfeed'><%= render 'feed' %></div>
<% end %>
feed.html.erb
<tr>
<th><%= sortable "price" %></th>
<th><%= sortable "created_at" %></th>
</tr>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<ol><%= render :partial => 'shared/item', :collection => @items %></ol>
Yes, you need to have :remote => true
in your @form_tag
if you want the submit to be done via ajax.
精彩评论