1

我正在尝试设置一个站点范围的搜索来搜索用户以及属于用户的项目。

基本上每个用户都有很多“谷物”,我希望能够将谷物隐藏在搜索结果中,并在其位置显示谷物的所有者。

现在我有太阳黑子功能,但是在我的用户索引页面上,它在执行搜索之前列出了所有用户。如果现在执行搜索,我希望结果为空。

我仍然是一个菜鸟,我正在努力解决这个问题。任何帮助将不胜感激。这是我的来源。

应用程序/模型/user.rb

class User < ActiveRecord::Base
    include Amistad::FriendModel
  mount_uploader :avatar, AvatarUploader
  has_many :cereals, dependent: :destroy

  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]


  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :avatar, :avatar_cache, :remove_avatar, :firstname, :lastname, :phone, :urlname
  # attr_accessible :title, :body

  attr_accessor :login

  def self.find_first_by_auth_conditions(warden_conditions)
      conditions = warden_conditions.dup
      if login = conditions.delete(:login)
        where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
      else
        where(conditions).first
      end
  end

            def update_with_password(params={})
        current_password = params.delete(:current_password)

        if params[:password].blank?
          params.delete(:password)
          params.delete(:password_confirmation) if params[:password_confirmation].blank?
        end

                result = if params[:password].blank? || valid_password?(current_password) 
          update_attributes(params)
        else
          self.attributes = params
          self.valid?
          self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
          false
        end

        clean_up_passwords
        result
            end

    searchable do
        text :firstname, :lastname, :username
    end

end

应用程序/控制器/users_controller.rb

  class UsersController < ApplicationController
        before_filter :authenticate_user!

        def index
            @search = User.search do
                fulltext params[:search]
            end
            @users = @search.results
      end

        def show
          if params[:id].nil? && current_user
            @user = current_user
          else
            @user = User.find(params[:id])
          end
          @cereal = current_user.cereals.build if signed_in?
          @cereals = @user.cereals.paginate(page: params[:page])
      end

      def first_time
          if params[:id].nil? && current_user
            @user = current_user
          else
            @user = User.find(params[:id])
          end
      end

        def edit
            if params[:id].nil? && current_user
                @user = current_user
            else
                @user = User.find(params[:id])
            end
      end

        def profile
            @username = params[:id]
            @friends = current_user.friends || []
            @title = "User Profile for #{@username}"
            @user = User.find_by_username(@username)
            @users = User.all :conditions => ["id != ?", current_user.id]
      end 

    end

意见/用户/index.html.haml

= form_tag @users_path, :method => :get do
        %p
            = text_field_tag :search, params[:search]
            = submit_tag "Search", name: 'nil'


    - @users.each do |user|
        %p
        = user.username
4

0 回答 0