I'm trying to implement faceted search with Sunspot, but I must be doing something wrong.
class Product < ActiveRecord::Base
attr_accessible :category, :color, :condition, :item_no, :size
searchable do
text :color, :size
end
end
This is my product model, i want to have a menu on the side of the navigation which will allow to filter results by using category, color, condition etc.
In my controller i have:
def index
#search = Product.search do
fulltext params[:search]
facet :color
end
#products = #search.results
respond_to do |format|
format.html
format.json { render json: #products }
end
end
But i keep getting an error that says:
Sunspot::UnrecognizedFieldError
No field configured for Product with name 'color'
In the view I have:
<div id="facets">
<h3>Category</h3>
<ul>
<% for row in #search.facet(:color).rows %>
<li>
<% if params[:color].blank? %>
<%= link_to row.value, :color => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :color => nil %>)
<% end %>
</li>
<% end %>
</ul>
</div>
Can anyone point what to what I'm doing wrong?
Thanks a lot for the help!
Update:
This is a piece of the trace of the error.
sunspot (1.3.3) lib/sunspot/query/restriction.rb:245:in `to_solr_conditional'
sunspot (1.3.3) lib/sunspot/query/restriction.rb:92:in `to_positive_boolean_phrase'
sunspot (1.3.3) lib/sunspot/query/restriction.rb:72:in `to_boolean_phrase'
sunspot (1.3.3) lib/sunspot/query/filter.rb:11:in `to_filter_query'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `block in to_params'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `map'
sunspot (1.3.3) lib/sunspot/query/scope.rb:5:in `to_params'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:51:in `block in to_params'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:50:in `each'
sunspot (1.3.3) lib/sunspot/query/common_query.rb:50:in `to_params'
sunspot (1.3.3) lib/sunspot/search/abstract_search.rb:37:in `execute'
sunspot_rails (1.3.3) lib/sunspot/rails/searchable.rb:329:in `solr_execute_search'
sunspot_rails (1.3.3) lib/sunspot/rails/searchable.rb:153:in `solr_search'
Two things missing here. First, you need to include the color as a field, and second, the facet method tells sunspot (i.e., solr) to return all the facets that are present in the result set, but not to scope the query by that facet. So, you need something like:
searchable do
text :color, :size
string :color
end
#search = Product.search do
fulltext params[:search]
with(:color).all_of(params[:color]) unless params[:color].blank?
facet :color
end
Related
I would appreciate any help why my test is failing. I am doing it based on Michael Hartl tutorial and I tried almost everything and read a lot of about it, but still clueless.
Tahnks
My test:
describe "pagination" do
before(:all) do
sign_in FactoryGirl.create(:user)
30.times { FactoryGirl.create(:user) }
visit users_path
end
after(:all) { User.delete_all }
it 'has the right div' do
page.should have_selector('div.pagination')
end
it "should list each user" do
User.paginate(page: 1).each do |user|
expect{page}.to have_selector('li', text: user.username)
end
end
My view:
<h1>Všichni uživatelé</h1>
<%= will_paginate %>
<ul class="users">
<% #users.each do |user| %>
<li>
<%= gravatar_for user%>
<%= link_to user.username, user %>
</li>
<% end %>
</ul>
<%= will_paginate(:renderer => BootstrapPagination::Rails)%>
My controller:
def index
#users = User.paginate(page: params[:page])
end
My error:
1) User pages index pagination should list each user
Failure/Error: expect{page}.to have_selector('li', text: user.username)
expected css "li" with text "Person 44" to return something
# ./spec/requests/user_pages_spec.rb:122:in `block (5 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:121:in `block (4 levels) in <top (required)>'
I think the line:
expect{page}.to have_selector('li', text: user.username)
Should be:
expect{page}.to have_selector('li', text: user.user.name)
trying to add pagination to my app (pages numbers at the bottom of the page)
getting error "undefined method `page' for #"
added 2 gems 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
ran bundle install & rails s
added the line to my app/controllers/pins_controller.rb
def index
#pins = Pin.order("created_at desc").page(params[:page]).per_page(20)
end
Added pagination to the pins index view - app/views/pins/index.html.erb
<%= render 'pages/home' %>
<div id="pins">
<%= render #pins %>
</div>
<%= will_paginate #pins %>
but get this error message
NoMethodError in PinsController#index
undefined method `page' for #
app/controllers/pins_controller.rb:6:in `index'
fixed ! Change <% will_paginate #pins %> to <%= will_paginate #pins %>
I had got a task to do pagination.When i am doing pagination i had got these error
NoMethodError in ProductsController#index
undefined method `page' for []:ActiveRecord::Relation
Rails.root: /home/nithinv/store
Application Trace | Framework Trace | Full Trace
app/controllers/products_controller.rb:4:in `index'
This is my controller
def index
#products = Product.order(:name).page(params[:page]).per(2)
respond_to do |format|
format.html #index.html.erb
format.json { render :json=> #products }
end
end
This is my index.html.erb
<% title "Products" %>
<%= paginate #products %>
<% for product in #products %>
<div class="product">
<h2><%= link_to product.name, product %></h2>
<div class="details">
<%= number_to_currency(product.price) %> |
Released <%= product.released_at.strftime("%B %e, %Y") %>
</div>
</div>
<% end %>
<p><%= link_to "New Product", new_product_path %></p>
How can i solve this problem?
Ref this
I think you forgot to add following line in your gemfile
gem 'kaminari'
and then bundle install
I looks that the problem has to do with kaminari gem configuration (not your code). I'd recommend you to do this quick check:
Run bundle install
Restart the server (because changes may not have been loaded)
Check your gemfile because, kaminari gem may is misplaced (under :test group)
A bit obvious, but in occasions I've also found myself wasting time after forgetting some basic checks.
I hope for help to follow problem.
I have install gem rails3-jquery-autocomplete from here
and gem "acts-as-taggable-on" from here
Here is my history:
my model is
class Article <ActiveRecord::Base
acts_as_taggable_on :tags
attr_accessible :title, :body, :tag_list
my controller is
class ArticlesController < ApplicationController
autocomplete :tag, :name, :class_name => 'ActAsTaggableOn::Tag'
def tag_cloud
#tags ||=Article.tag_counts_on(:tags)
end
def all
#tags = Article.tag_counts_on(:tags).limit(8).order('count desc')
klass = Article
klass = klass.tagged_with(#tag) if (#tag = params[:tag]).present?
#articles = klass.where(:state => '4').paginate(:page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => #articles }
end
end
my route is
resources :articles do
collection do
get 'autocomplete_tag_name'
get 'about'
get 'all'
get 'myarticles'
delete 'destroy'
end
end
my _form.html.erb
<div class="field">
<label for="tag_list">Κατηγορία (Μπορείτε να προσθέσετε διαφορετικές κατηγορίες διαχωρίζοντάς τες με κόμμα)</label><br />
<%= f.autocomplete_field :tag_list, autocomplete_tag_name_articles_path, :"data-delimiter" => ', ' %>
</div>
my index.html.erb
<div class='tag-box'>
<% #tags.sort_by(&:count).reverse.each do |k| %>
<% url_opts = {:action => "all", :controller => "articles"}
link_name = "#{k.name} (#{k.count})" %>
<% if #tag == k.name %>
<%= link_to link_name, url_opts.merge(:tag => nil), :class => "tag current_tag", :title => "Κλικ για εμφάνιση όλων" %>
<% else %>
<%= link_to link_name, url_opts.merge(:tag => k.name), :class => "tag", :title => "Κλικ για εμφάνιση άρθρων στην κατηγορία #{k.name}" %>
<% end %>
<% end %>
</div>
my application.html.erb
<%= stylesheet_link_tag :all %>
<%= javascript_include_tag :defaults %>
<%= javascript_include_tag 'jquery-1.7.2.min.js', 'jquery-ui-1.8.19.custom.min.js', 'autocomplete-rails.js', 'rails.js' %>
<%= stylesheet_link_tag 'jquery-ui-1.8.19.custom.css' %>
and last my Gemfile
gem 'rails', '3.0.11'
gem 'pg'
gem 'jquery-rails', '>= 1.0.12'
gem 'rails3-jquery-autocomplete'
gem 'acts-as-taggable-on', '~> 2.2.2'
in app works the tagging system but don't work the autocomplete.
perhaps my problem is on file _form.html.erb. I have on file new.html.erb the code
<section id="myarticles">
<h2>Νέο άρθρο</h2>
<%= render 'form' %>
</section>
Is about render and autocomplete together and autocomplete don't work?
Finaly the solution for my problem that can help someone with the same situation is simple.
Copy the file jquery-ui-1.8.19.custom.css from public/stylesheets/ui-lightness or from public/stylesheets/smoothness depends from the download theme from jQuery UI-Autocomplete
and paste to public/stylesheets/ and automagicaly disappeared the Routing Error and voila the Autocomplete functioning.
I had follow the instructions from here
Copy the files on the css folder to the public/stylesheets folder on your app. Note that these files may be one level down from the css folder, in a folder called "ui-lightness".
but the right position is on directory public/stylesheets.
Also remove from mycontroller the follow code :class_name => 'ActAsTaggableOn::Tag' and now the code looks like
class ArticlesController < ApplicationController
autocomplete :tag, :name
I'm totally stumped on this error. Would really appreciate some help :).
To reproduce the error, you can pull the program from https://github.com/WaleyChen/twitter_clone. Then run 'bundle exec rspec spec/'.
I have an rspec test for my controller defined as:
require 'spec_helper'
describe FrontpageController do
render_views # render the views inside the controller tests, so not just test the actions
describe "GET 'frontpage'" do
it "should be successful" do
get 'frontpage'
response.should be_success
end
it "should have the right title" do
get 'frontpage'
response.should have_selector("title", :content => "Twitter")
end
end
end
When I run my rspec tests, I get the following error:
Failures:
1) FrontpageController GET 'frontpage' should be successful
Failure/Error: get 'frontpage'
ActionView::Template::Error:
undefined method `full_name' for #<User:0x007fbfce43dce0>
# ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./spec/controllers/frontpage_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
2) FrontpageController GET 'frontpage' should have the right title
Failure/Error: get 'frontpage'
ActionView::Template::Error:
undefined method `full_name' for #<User:0x007fbfcc99a410>
# ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
# ./spec/controllers/frontpage_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
Here's the controller:
class FrontpageController < ApplicationController
def frontpage
#user = User.new
#sign_up = User.new
end
def sign_up
if #post.save
format.html { redirect_to #post, notice: 'Post was successfully created.' }
end
end
end
Here's the view, that's causing the error:
<%= form_for #user, :url =>"sign_up" do |form| %>
<%= form.text_field :full_name, :placeholder => "Full name" %>
</br>
<%= form.text_field :email, :placeholder => "Email" %>
</br>
<%= form.text_field :pw, :placeholder => "Password" %>
</br>
<%= form.text_field :username, :placeholder => "Username" %>
</br>
<%= form.submit "Sign up" %>
<% end %>
Here's user.rb:
class User < ActiveRecord::Base
end
Here's schema.rb:
ActiveRecord::Schema.define(:version => 20111106084309) do
create_table "users", :force => true do |t|
t.string "full_name"
t.string "email"
t.string "username"
t.string "pw"
t.datetime "created_at"
t.datetime "updated_at"
end
end
You need to make sure your test database is up to date with all the migrations:
rake db:test:prepare
However, you may have a problem with that as your migrations are broken; you have two migrations named "CreateUsers" which Rails will complain about. It looks like you should delete the more recent one, and then uncomment the t.string :email line in the original one.
Also, if you use bundle exec rake rspec instead of bundle exec rspec spec it'll make sure your database migrations are up to date before running the tests. I just cloned your repo and did this and it passed the tests just fine.