我正在尝试将思维狮身人面像与高级搜索表单结合起来。我对如何在控制器中设置动作感到困惑。模型,以便它可以显示结果。当我从主页搜索某些内容时,我得到一个未定义的方法“搜索”错误,指向搜索控制器:
def index @users = User.search(params [:search]) 端
我只使用用户模型,因为我希望能够搜索我的用户个人资料,例如他们的年龄,性别,种族,关于我的部分等。
搜索控制器:
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
用户模型:
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(在indices文件夹中):
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
搜索表单:
<%= 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>
正在努力学习TS是如何工作的,我们将非常感谢正确方向上的一点。这似乎并不太难,但不知怎的,我正在抓麻烦。
答案 0 :(得分:1)
请考虑以下事项:
Class Foo
def self.a_class_method
end
def an_instance_method
end
end
Foo.a_class_method
Foo.new.an_instance_method
您现在可以理解这没有意义:
def search
@search_users = User.search(params[:search])
end
您的模型中也无法访问params
。你需要通过参数传递它。
我对斯芬克斯不是很熟悉,但你现在可以让它发挥作用。
您的问题是,您需要为search
模型定义类User
方法才能执行搜索:
class User
def self.search(criteria)
# do the search here base on the criteria
end
end
在您的控制器中:
User.search(params[:search])