Anders Andrew
2012-Jun-24 11:16 UTC
How do I sort my columns inside my AJAX search result?
I have just converted into sunspot but am having problems geting my
sorting of columns to work in the controller. My old controller action
looked like this:
def index
@user = current_user
@products = @user.products.search(params[:search]).order(sort_column +
'' '' + sort_direction)
end
However I haven''t been able to apply this into my new controller
action.
Any ideas?
My complete application:
models/product.rb:
class Product < ActiveRecord::Base
attr_accessible :ean, :name, :price, :stock, :user_id, :sku
belongs_to :user
validates :user_id, presence: true
searchable do
text :ean, :sku, :name
end
end
controllers/products_controller.rb:
class ProductsController < ApplicationController
helper_method :sort_column, :sort_direction
def index
@user = current_user
@search = @user.products.search do
fulltext params[:search]
end
@products = @search.results
end
private
def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] :
"name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] :
"asc"
end
end
views/products/index.html.erb:
<%= form_tag products_path, :method => :get, :id =>
"products_search" do
%>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil, class: "btn
btn-large
btn-primary" %>
</p>
<div id="products"><%= render ''products''
%></div>
<% end %>
views/index.js.erb:
$(''#products'').html(''<%=
escape_javascript(render("products")) %>'');
views/products/_products.html.erb:
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<table class="pretty">
<tr>
<th><%= sortable "sku", "SKU" %></th>
<th><%= sortable "name", "Name"
%></th>
<th><%= sortable "stock", "Stock"
%></th>
<th><%= sortable "price", "Price"
%></th>
<th><%= sortable "ean", "EAN" %></th>
</tr>
<% for product in @products %>
<tr>
<td><%= product.sku %></td>
<td><%= product.name %></td>
<td><%= product.stock %></td>
<td><%= product.price %></td>
<td><%= product.ean %></td>
</tr>
<% end %>
</table>
assets/javascripts/application.js:
$(function () {
$(''#products th a'').live(''click'',
function () {
$.getScript(this.href);
return false;
}
);
$(''#products_search input'').keyup(function () {
$.get($("#products_search").attr("action"),
$("#products_search").serialize(), null, ''script'');
return false;
});
});
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
radhames brito
2012-Jun-24 11:58 UTC
Re: How do I sort my columns inside my AJAX search result?
> > > def index > @user = current_user > @search = @user.products.search do > fulltext params[:search] > end > @products = @search.results > end > >if @search is not an ActiveRecord::Relation object you cant chain it with another ActiveRecord::Relation object. What class does the search function returns? are you using a gem? the search function could be returning an array in which case you have to build your own sorting mechanism. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anders Andrew
2012-Jun-24 12:14 UTC
Re: How do I sort my columns inside my AJAX search result?
Radhames Brito wrote in post #1065828:>> > if @search is not an ActiveRecord::Relation object you cant chain it > with > another ActiveRecord::Relation object. What class does the search > function > returns? are you using a gem? the search function could be returning an > array in which case you have to build your own sorting mechanism.Hi rbritom. I am using the sunspot_rails gem (https://github.com/sunspot/sunspot). My old search function (and sorting function) was build after Railscast 240 (http://railscasts.com/episodes/240-search-sort-paginate-with-ajax), but as I needed to search through multiple columns I implemented Sunspot instead. Not quite sure if @search is an ActiveRecord::Relation. Fairly new to Rails. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anders Andrew
2012-Jun-24 12:53 UTC
Re: How do I sort my columns inside my AJAX search result?
Anders Andrew wrote in post #1065829:> Radhames Brito wrote in post #1065828: >>> >> if @search is not an ActiveRecord::Relation object you cant chain it >> with >> another ActiveRecord::Relation object. What class does the search >> function >> returns? are you using a gem? the search function could be returning an >> array in which case you have to build your own sorting mechanism. > > Hi rbritom. > > I am using the sunspot_rails gem (https://github.com/sunspot/sunspot). > > My old search function (and sorting function) was build after Railscast > 240 (http://railscasts.com/episodes/240-search-sort-paginate-with-ajax), > but as I needed to search through multiple columns I implemented Sunspot > instead. Not quite sure if @search is an ActiveRecord::Relation. Fairly > new to Rails.When I look around on the internet I can see that people uses sort_by to make it work, e.g.: def index @user = current_user @search = @user.products.search do fulltext params[:search] order_by sort_column, sort_direction end @products = @search.results end However it stil breaks. With this error message: Sunspot::UnrecognizedFieldError in ProductsController#index No field configured for Product with name ''name'' -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.