Reputation: 1395
I am trying to combine thinking sphinx with the advanced search form. I am getting stuck on how to setup the action in the controller & model so it can display the results. When I search for something from the homepage I get a undefined method `search' error that points to searches controller:
def index @users = User.search(params[:search]) end
I am only using the User model, as I want to be able to search my users profile such as their age, gender, ethnicity, about me section, etc.
Searches controller:
class SearchesController < ApplicationController
def new
@search = Search.new
end
def create
@search = Search.new(params[:search])
if @search.save
redirect_to @search
else
render 'new'
end
end
def show
@search = Search.find(params[:id])
@users = @search.users
end
def index
@users = User.search(params[:search])
end
end
User model:
class User < ActiveRecord::Base
has_secure_password
attr_accessible :role, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
has_many :photos
validates_uniqueness_of :email
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create
validates_presence_of :password, :on => :create
before_create { generate_token(:auth_token) }
ROLES = %w[admin user guest banned]
# models/user.rb
after_create :setup_gallery
def search
@search_users = User.search(params[:search])
end
def received_messages
Message.received_by(self)
end
def unread_messages?
unread_message_count > 0 ? true : false
end
def unread_messages
received_messages.where('read_at IS NULL')
end
def sent_messages
Message.sent_by(self)
end
# Returns the number of unread messages for this user
def unread_message_count
eval 'messages.count(:conditions => ["recipient_id = ? AND read_at IS NULL", self.user_id])'
end
def to_s; username
end
def has_role?(role_name)
role.present? && role.to_sym == role_name.to_sym
end
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
private
def setup_gallery
self.galleries << Gallery.create
end
end
User_index (inside indices folder):
ThinkingSphinx::Index.define :user, :with => :active_record do
# fields
indexes name, :as => :user, :sortable => true
indexes [ethnicity, religion, about_me, sexuality, children, user_smoke, user_drink, age, gender]
# attributes
has id, created_at, updated_at
end
Search form:
<%= form_tag searches_path, method: :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= button_tag "Search", name: nil %>
</p>
<% end %>
<P><%= link_to "Advanced Search", new_search_path %><p>
Just a point in the right direction would be greatly appreciated as I am struggling to learn how TS works. It doesn't seem too difficult, but somehow I am having troubles grasping it.
Upvotes: 0
Views: 2349
Reputation: 17631
Please consider the following:
Class Foo
def self.a_class_method
end
def an_instance_method
end
end
Foo.a_class_method
Foo.new.an_instance_method
You can now understand that this do not make sense:
def search
@search_users = User.search(params[:search])
end
Also params
will not be accessible in your model. You need to pass it through arguments.
I am not very familiar with Sphinx but you might be able to make it work now.
Your issue is that you need to define a class search
method for your User
model to perform your search:
class User
def self.search(criteria)
# do the search here base on the criteria
end
end
In your controller:
User.search(params[:search])
Upvotes: 1