我正在尝试将思维狮身人面像与高级搜索表单相结合。我被困在如何在控制器和模型中设置动作以便它可以显示结果。当我从主页搜索某些内容时,我得到一个指向搜索控制器的未定义方法“搜索”错误:
def index @users = User.search(params[:search]) end
我只使用用户模型,因为我希望能够搜索我的用户个人资料,例如他们的年龄、性别、种族、关于我的部分等。
搜索控制器:
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(在索引文件夹内):
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 的工作原理,因此只要在正确的方向上指出一点将不胜感激。这似乎不太难,但不知何故我很难掌握它。