add blog search feature
This commit is contained in:
parent
9de1aa67d4
commit
a2873718bb
|
@ -11,12 +11,22 @@
|
||||||
margin-right: 1rem;
|
margin-right: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.search-result-wrapper p {
|
||||||
|
font-size: 0.785rem;
|
||||||
|
color: #999;
|
||||||
|
}
|
||||||
|
|
||||||
.blog-title {
|
.blog-title {
|
||||||
color: #111111;
|
color: #111111;
|
||||||
&:hover {
|
&:hover {
|
||||||
color: red;
|
color: red;
|
||||||
}
|
}
|
||||||
border: none;
|
border: none;
|
||||||
|
|
||||||
|
em {
|
||||||
|
color: red;
|
||||||
|
font-style: normal;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
li {
|
li {
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
class ArchivesController < ApplicationController
|
class ArchivesController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@posts = Post.order(created_at: :desc).page(params[:page])
|
if (@q = params[:q]).blank?
|
||||||
|
@posts = Post.order(created_at: :desc).page(params[:page])
|
||||||
|
else
|
||||||
|
@q_size = Post.where('title like ?', "%#{@q}%").size
|
||||||
|
@posts = Post.where('title like ?', "%#{@q}%").order(created_at: :desc).page(params[:page])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,3 +1,9 @@
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
# some format function is defined in app/controllers/application_controller.rb
|
# some format function is defined in app/controllers/application_controller.rb
|
||||||
|
#
|
||||||
|
def search_highlight(title, q)
|
||||||
|
return title if q.blank?
|
||||||
|
|
||||||
|
title.sub(q, "<em>#{q}</em>")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,9 +3,13 @@
|
||||||
.row
|
.row
|
||||||
.small-12.large-9.large-centered.columns
|
.small-12.large-9.large-centered.columns
|
||||||
ul.archives-field
|
ul.archives-field
|
||||||
|
.search-wrapper
|
||||||
|
= form_with url: archives_path, method: 'GET' do |f|
|
||||||
|
= f.search_field :q, value: @q, placeholder: t('archive.search'), autofocus: @q.blank?
|
||||||
- @posts.each do |post|
|
- @posts.each do |post|
|
||||||
li
|
li
|
||||||
= link_to post.title, blog_path(post), class: 'blog-title'
|
= link_to blog_path(post), class: 'blog-title' do
|
||||||
|
== search_highlight(post.title, @q)
|
||||||
p.tags-field
|
p.tags-field
|
||||||
i.fi-calendar
|
i.fi-calendar
|
||||||
span
|
span
|
||||||
|
@ -19,4 +23,7 @@
|
||||||
i.fi-heart
|
i.fi-heart
|
||||||
span
|
span
|
||||||
= post.liked_count
|
= post.liked_count
|
||||||
= paginate @posts
|
- if @q.present?
|
||||||
|
.search-result-wrapper
|
||||||
|
p.text-muted 共 #{@q_size || 0} 条结果
|
||||||
|
= paginate @posts, q: @q
|
||||||
|
|
|
@ -55,6 +55,10 @@ html
|
||||||
= link_to t('head.timeline'), archives_path
|
= link_to t('head.timeline'), archives_path
|
||||||
li
|
li
|
||||||
= link_to t('head.about'), about_path
|
= link_to t('head.about'), about_path
|
||||||
|
li
|
||||||
|
= link_to archives_path do
|
||||||
|
span
|
||||||
|
i.fi-magnifying-glass
|
||||||
- flash.each do |name, msg|
|
- flash.each do |name, msg|
|
||||||
- if msg.is_a?(String)
|
- if msg.is_a?(String)
|
||||||
div class=("callout #{name.to_sym == :notice ? "success" : "alert"}") data-closable=""
|
div class=("callout #{name.to_sym == :notice ? "success" : "alert"}") data-closable=""
|
||||||
|
|
|
@ -55,6 +55,9 @@ en:
|
||||||
publish_success: 'Publish Successfully'
|
publish_success: 'Publish Successfully'
|
||||||
publish_fail: 'Publish Failed'
|
publish_fail: 'Publish Failed'
|
||||||
|
|
||||||
|
archive:
|
||||||
|
search: 'Search'
|
||||||
|
|
||||||
admin:
|
admin:
|
||||||
new_post: 'New Post'
|
new_post: 'New Post'
|
||||||
posts: 'Manage Posts'
|
posts: 'Manage Posts'
|
||||||
|
|
|
@ -55,6 +55,9 @@ zh-CN:
|
||||||
publish_success: '发表成功'
|
publish_success: '发表成功'
|
||||||
publish_fail: '发表失败'
|
publish_fail: '发表失败'
|
||||||
|
|
||||||
|
archive:
|
||||||
|
search: '搜索博客标题'
|
||||||
|
|
||||||
admin:
|
admin:
|
||||||
new_post: '新建博客'
|
new_post: '新建博客'
|
||||||
posts: '博客管理'
|
posts: '博客管理'
|
||||||
|
|
Loading…
Reference in New Issue