rails 4 中的多个搜索字段

Multiple search fields in rails 4

使用 Rails 4 和 ruby 2.2, 我的用户模型具有以下属性

  1. username
  2. email
  3. active

现在我需要为用户名、电子邮件创建一个搜索文本字段,并为活动属性创建一个下拉列表。我有我的

user.rb

  def self.usernames(username)
      user = all
      user = user.where("username LIKE ?", "%#{username}%")
      return user
  end


  def self.emails(email)
    user = all
    user = user.where("email LIKE ?", "%#{email}%")
    return user
  end


  def self.actives(active)
    user = all
    user = user.where("active LIKE ?", "%#{active}%")
    return user

users_controller.rb

 def index
    @users = User.all
    @users = @users.usernames(params[:username]) if params[:username].present?
    @users = @users.emails(params[:email]) if params[:email].present?
    @users = @users.actives(params[:active]) if params[:active].present?
  end

index.html.erb

h1>Listing Users</h1>

<div id="forms">
    <%= form_tag users_path, :method=> "get" do %>

      <%= label_tag :username, "Name" %>
      <%= text_field_tag :username %>

      <%= label_tag :email, "Email" %>
      <%= text_field_tag :email%>

      <%= label_tag :active, "Active" %>
      <%= select_tag :active, options_for_select([['Active', true], ['Inactive', false]]) %>
      <%= submit_tag "Search" %> 
      <%= link_to "Clear Search", request.path, class:"cancel-button" %>
    <% end %>

</div>
<div id="users"><%= render "users" %></div>


<%= button_to "Create User", new_user_path, method: :get %>

Schema.rb

 create_table "users", force: :cascade do |t|
    t.string   "email",                  limit: 255, default: "",    null: false
    t.string   "encrypted_password",     limit: 255, default: "",    null: false
    t.string   "reset_password_token",   limit: 255
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.string   "name",                   limit: 255
    t.integer  "sign_in_count",          limit: 4,   default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip",     limit: 255
    t.string   "last_sign_in_ip",        limit: 255
    t.string   "confirmation_token",     limit: 255
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email",      limit: 255
    t.integer  "failed_attempts",        limit: 4,   default: 0,     null: false
    t.string   "unlock_token",           limit: 255
    t.datetime "locked_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "username",               limit: 255
    t.boolean  "active",                             default: false

现在,当我尝试按用户名或电子邮件进行过滤时,它没有给我任何结果。

以下是控制台的结果,

Started GET "/users?utf8=%E2%9C%93&username=alok&email=&active=true&commit=Search" for ::1 at 2016-04-18 13:10:09 +0530
Processing by UsersController#index as HTML
  Parameters: {"utf8"=>"✓", "username"=>"alok", "email"=>"", "active"=>"true", "commit"=>"Search"}
  User Load (1.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = ?  ORDER BY `users`.`id` ASC LIMIT 1  [["id", 11]]
{"utf8"=>"✓", "username"=>"alok", "email"=>"", "active"=>"true", "commit"=>"Search", "controller"=>"users", "action"=>"index"}
  User Load (1.0ms)  SELECT `users`.* FROM `users` WHERE (username LIKE '%alok%') AND (active LIKE '%true%')
  Rendered users/_users.html.erb (7.2ms)
  Rendered users/index.html.erb within layouts/application (49.3ms)
Completed 200 OK in 671ms (Views: 550.7ms | ActiveRecord: 49.9ms)

我参考了以下 post 但得到了类似的结果,没有输出,如果我遗漏了什么请告诉我,

Multiple search fields in rails

看起来问题出在函数 def self.actives(active)

尝试将其更改为

 def self.actives(active)
    user = all
    user = user.where("active = ?", active)
    return user
end

您可能还想尝试 Ransack gem 以便轻松 add/remove 搜索表单中的字段 https://github.com/activerecord-hackery/ransack