使用Rails 4和ruby 2.2, 我的用户模型具有以下属性
- 用户名
- 电子邮件
- 活性
醇>
现在,我需要为用户名,电子邮件和下拉列表创建活动属性的搜索文本字段。我有我的
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)
我已经推荐了以下帖子,但得到了类似的结果,没有输出,如果我遗漏任何内容,请告诉我,
答案 0 :(得分:0)
看起来问题出在函数def self.actives(active)
中尝试将其更改为
Route::group(['middleware'=>['web']],function(){
Route::resource('test','testController');
});
您可能还想尝试使用Ransack gem,以便在搜索表单中轻松添加/删除字段 https://github.com/activerecord-hackery/ransack