【问题标题】:case sensitive search is limiting results区分大小写的搜索限制了结果
【发布时间】:2015-03-21 23:02:30
【问题描述】:

我正在尝试修改我的搜索,以便如果用户搜索“牧马人”,他们将获得与搜索“牧马人”相同的结果。数据库的第一个字母大写,所以我想如果小写的话我需要大写我的搜索..不知道如何做到这一点---基本上,我不希望大写在搜索时发挥作用。

我的搜索表单:

<%= 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 %>

我的帖子控制器:

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/

【问题讨论】:

  • 您使用的是哪个数据库?
  • Sqlite3 local / pg live on heroku

标签: ruby-on-rails ruby search


【解决方案1】:

这是一个小例子。

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

这里发生了什么 lower(field_name) 使字段的值小写

params[:name].downcase 使值小写

【讨论】:

  • 我是否将其添加为新行?
  • 是的,您可以在新行中执行此操作。确保更改您想要不敏感的字段的名称
  • 好的,得到一个动作控制器错误......我想要不敏感的字段是“模型”......我试过这个......@post = @post.where([“lower(name) = ?", params[:model].downcase])
  • 再看答案我加了:all, :conditions =>
  • 好的..所以我需要改变什么吗?我在哪里插入“模型”。?抱歉,我是 Rails 初学者...
【解决方案2】:

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

【讨论】:

  • 搜索已提交,但无法正常工作..我相信的一切都与 def...
猜你喜欢
  • 2018-02-28
  • 2013-06-15
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多