简体   繁体   English

区分大小写的搜索限制了结果

[英]case sensitive search is limiting results

I am trying to modify my search so that if a user searches "wrangler" they will get the same results as searching "Wrangler". 我正在尝试修改搜索,以便如果用户搜索“牧马人”,他们将获得与搜索“牧马人”相同的结果。 The database has the first letter capitalized so I guess I need to upcase my search if lowercase .. not sure how to accomplish this --- basically, I do not want the capitalization to come into play at all when searching. 数据库的首字母大写,因此我猜如果小写的话我需要大写..不确定如何完成此操作---基本上,我不希望大写在搜索时起作用。

my search form: 我的搜索表:

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :heading %><br>
    <%= f.text_field :heading %>
  </div>
  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </div>
  <div class="field">
    <%= f.label :price %><br>
    <%= f.text_field :price %>
  </div>
  <!--<div class="field">
    <%= f.label :neighborhood %><br>
    <%= f.text_field :neighborhood %>
  </div> -->
  <div class="field">
    <%= f.label :external_url %><br>
    <%= f.text_field :external_url %>
  </div>
  <div class="field">
    <%= f.label :timestamp %><br>
    <%= f.text_field :timestamp %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

my posts controller: 我的帖子控制器:

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

def home
end

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.order('timestamp DESC').paginate(:page => params[:page], :per_page => 50)
    @posts = @posts.where(model: params["model"]) if params["model"].present?
    @posts = @posts.where(year: params["year"]) if params["year"].present?
    @posts = @posts.where(neighborhood: params["neighborhood"]) if params["neighborhood"].present?
    @posts = @posts.where("price > ?", params["min_price"]) if params["min_price"].present?
    @posts = @posts.where("price < ?", params["max_price"]) if params["max_price"].present?
    @posts = @posts.where(transmission: params["transmission"]) if params["transmission"].present?
    @posts = @posts.where(title_status: params["title_status"]) if params["title_status"].present?
  end

  # GET /posts/

Here is a small example. 这是一个小例子。

@posts = @posts.where( all, :conditions => ["lower(model) =?", params["model"].downcase]) if params["model"].present?

What happens here lower(field_name) makes the value of the field lower case 这里发生的情况lower(field_name)使字段的值变成小写

params[:name].downcase makes the value lower case params [:name] .downcase使值小写

@posts = @ posts.where(all,:conditions => [“” lower(model)=?“,params [” model“]。downcase])如果params [” model“]。present?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM