From c2814a6b0f2bf743f72cbc8b44285463870f87d6 Mon Sep 17 00:00:00 2001 From: Peter Wang Date: Sun, 28 Sep 2014 12:08:40 +0000 Subject: [PATCH] search works --- esdemo/app/assets/stylesheets/users.css.scss | 8 ++++++++ esdemo/app/controllers/users_controller.rb | 11 +++++++++++ esdemo/app/models/user.rb | 2 ++ esdemo/app/views/users/search.html.erb | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 esdemo/app/views/users/search.html.erb diff --git a/esdemo/app/assets/stylesheets/users.css.scss b/esdemo/app/assets/stylesheets/users.css.scss index 699dff0..3942784 100644 --- a/esdemo/app/assets/stylesheets/users.css.scss +++ b/esdemo/app/assets/stylesheets/users.css.scss @@ -21,3 +21,11 @@ .new-user { margin-top: 50px; } + + +.search-result-page { + .back { + margin-top: 30px; + display: block; + } +} diff --git a/esdemo/app/controllers/users_controller.rb b/esdemo/app/controllers/users_controller.rb index e2e33a0..f6fd836 100644 --- a/esdemo/app/controllers/users_controller.rb +++ b/esdemo/app/controllers/users_controller.rb @@ -22,6 +22,17 @@ def destroy redirect_to :root end + def search + @users = User.search( + query: { + multi_match: { + query: params[:q].to_s, + fields: ['name', 'intro'] + } + } + ).records + end + private def user_params params.require(:user).permit(:intro, :name) diff --git a/esdemo/app/models/user.rb b/esdemo/app/models/user.rb index 4a57cf0..6dd191e 100644 --- a/esdemo/app/models/user.rb +++ b/esdemo/app/models/user.rb @@ -1,2 +1,4 @@ class User < ActiveRecord::Base + include Elasticsearch::Model + include Elasticsearch::Model::Callbacks end diff --git a/esdemo/app/views/users/search.html.erb b/esdemo/app/views/users/search.html.erb new file mode 100644 index 0000000..733857b --- /dev/null +++ b/esdemo/app/views/users/search.html.erb @@ -0,0 +1,19 @@ +
+ + + + + + + <% @users.each do |u| %> + + + + + <% end %> +
NameIntro
<%= u.name %><%= u.intro %>
+ + <%= link_to root_path, class: "back" do %> + Back + <% end %> +