我正在做一个高级搜索系统,但我没有结果,搜索根本不起作用,我是rails的新手,从未应用过它。
我的型号
class Product < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]
belongs_to :category
belongs_to :user
def self.search(params)
conditions_query = []
conditions_value = []
if params[:title].present?
conditions_query << 'title LIKE ?'
conditions_value << "%#{params[:title]}%"
end
if params[:category].present?
conditions_query << 'category LIKE ?'
conditions_value << "%#{params[:category]}%"
end
if params[:price].present?
conditions_query << 'price LIKE ?'
conditions_value << "%#{params[:price]}%"
end
conditions = [conditions_query.join(" AND "), conditions_value]
end
end
我的控制器:
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
before_action :set_user, only: [:show, :edit, :update, :destroy, :create, :update, :new]
# GET /products
# GET /products.json
def index
@products = Product.all
@user = User.all
end
# GET /products/1
# GET /products/1.json
def show
end
# GET /products/new
def new
@product = Product.new
end
# GET /products/1/edit
def edit
end
# POST /products
# POST /products.json
def create
@product = Product.new(product_params)
respond_to do |format|
if @product.save
@product.update(user_id: params[:user_id])
format.html { redirect_to user_products_path(@user.id ,params[:product_id]), notice: 'Product was successfully created.' }
format.json { render 'show', status: :created, location: @product }
else
format.html { render :new }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @product.update(product_params)
format.html { redirect_to user_product_path, notice: 'Product was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
# PATCH/PUT /products/1
# PATCH/PUT /products/1.json
# DELETE /products/1
# DELETE /products/1.json
def destroy
@product.destroy
respond_to do |format|
format.html { redirect_to user_products_url, notice: 'Product was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :price, :local, :description, :category_id, :contacts, :image, :user_id)
end
def set_user
@user = User.find(params[:user_id])
end
end
我的应用程序:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
helper_method :current_user
helper_method :cate
def cate
Category.all
end
private
def current_user
User.find_by id: session[:user_id] if session[:user_id] if session[:user_id]
end
end
看起来你走在了正确的轨道上,但搜索方法实际上并不执行任何查询,即使你已经构建了所有条件。对最后一行进行小的调整,你可以得到这样的结果:
def self.search(params)
conditions_query = []
conditions_value = []
if params[:title].present?
conditions_query << 'title LIKE ?'
conditions_value << "%#{params[:title]}%"
end
if params[:category].present?
conditions_query << 'category LIKE ?'
conditions_value << "%#{params[:category]}%"
end
if params[:price].present?
conditions_query << 'price LIKE ?'
conditions_value << "%#{params[:price]}%"
end
where(conditions_query.join(" AND "), *conditions_value)
end
您缺少where
,应该将conditions_value数组泼掉。
尽管我认为写这篇文章的更好方法是使用这样的范围:
scope :title_like, -> (title) { where('title LIKE ?', "%#{title}%") if title.present? }
scope :category_like, -> (category) { where('category LIKE ?', "%#{category}%") if category.present? }
scope :price_like, -> (price) { where('price LIKE ?', "%#{price}%") if price.present? }
def self.search(params)
title_like(params[:title])
.category_like(params[:category])
.price_like(params[:price])
end