Open chocomint opened 12 years ago
MetaWhere & MetaSearchを使うと簡単に実装できる。 http://railscasts.com/episodes/251-metawhere-metasearch?language=ja&view=asciicast
diff --git a/Gemfile b/Gemfile
index 0dcca69..1b6328f 100755
--- a/Gemfile
+++ b/Gemfile
@@ -17,3 +17,4 @@ gem 'RedCloth'
gem "twitter-bootstrap-rails"
gem "google-code-prettify-rails"
gem "kaminari"
+gem "meta_search"
diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css
index b028cd1..0779859 100755
--- a/app/assets/stylesheets/application.css
+++ b/app/assets/stylesheets/application.css
@@ -36,3 +36,11 @@ div.main {
.right {
text-align: right;
}
+
+div.sidebar-nav form.form-search input.search-query {
+ margin-bottom: 5px;
+}
+
+div.sidebar-nav form.form-search {
+ text-align: right;
+}
diff --git a/app/controllers/entries_controller.rb b/app/controllers/entries_controller.rb
index b05bfa2..87e2e0f 100644
--- a/app/controllers/entries_controller.rb
+++ b/app/controllers/entries_controller.rb
@@ -1,8 +1,14 @@
# vim:fileencoding=UTF-8
class EntriesController < ApplicationController
+ before_filter :load_search, :only => [:index, :show, :edit]
def index
- @entry = Entry.new
- @entries = Entry.page(params[:page]).per(params[:per]).newly
+ @entries = Entry.newly
+ if params[:search] then
+ @entries = @entries.search(params[:search])
+ else
+ @entry = Entry.new
+ end
+ @entries = @entries.page(params[:page]).per(params[:per])
end
def show
@@ -50,4 +56,9 @@ class EntriesController < ApplicationController
end
redirect_to entries_path
end
+
+ private
+ def load_search
+ @search = Entry.search(params[:search])
+ end
end
diff --git a/app/models/entry.rb b/app/models/entry.rb
index 679c06c..28bc27e 100644
--- a/app/models/entry.rb
+++ b/app/models/entry.rb
@@ -1,6 +1,7 @@
# vim:fileencoding=UTF-8
class Entry < ActiveRecord::Base
attr_accessible :body
+ attr_searchable :body
attr_accessor :more
paginates_per 30
diff --git a/app/views/entries/index.html.erb b/app/views/entries/index.html.erb
index 774220d..44669d4 100644
--- a/app/views/entries/index.html.erb
+++ b/app/views/entries/index.html.erb
@@ -1,3 +1,4 @@
+<%- if @entry -%>
<%= render :partial => 'layouts/errors', :locals => {:object => @entry} %>
<div class="hero-unit">
<%= form_for(@entry) do |f| %>
@@ -12,6 +13,7 @@
</div>
<% end %>
</div>
+<%- end -%>
<%= render :partial => 'entries/entry', :collection => @entries, :as => :entry, :locals => {:is_open => false} %>
<%= paginate @entries %>
diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb
index c4a1239..6e79b74 100644
--- a/app/views/layouts/_sidebar.html.erb
+++ b/app/views/layouts/_sidebar.html.erb
@@ -1,3 +1,9 @@
+<h4>検索</h4>
+<%= form_for @search, :url => entries_path, :method => :get, :html => {:class => "form-search"} do |f| %>
+ <%= f.search_field(:body_contains, :class => "input-medium search-query") %>
+ <%= f.submit("検索", :class => "btn") %>
+<% end %>
+
<h4>新着記事一覧</h4>
<ul class="nav nav-list">
<%- recent_entries.inject(nil) do |current_date, entry| -%>
過去の記事を文字列検索できるようにする。
検索結果では、検索文字列がハイライト表示されると望ましい。