diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb new file mode 100644 index 0000000..91d60a1 --- /dev/null +++ b/app/controllers/search_controller.rb @@ -0,0 +1,28 @@ +# coding: utf-8 +class SearchController < ApplicationController + + def index + if params[:format] == "json" + result = Ask.search_title(params["w"],:limit => 10) + puts result[:words] + simple_items = result[:items].collect do |item| + {:topics => item.topics, + :title => item.title, + :_id => item.id} + end + render :json => simple_items.to_json + else + @asks = Ask.search_title(params["w"],:limit => 20)[:items] + set_seo_meta("关于“#{params[:w]}”的搜索结果") + render "/asks/index" + end + end + + def topics + if params[:format] == "json" + @topics = Topic.search_name(params[:w],:limit => 10) + render :json => @topics.to_json(:only => [:id,:name]) + end + + end +end diff --git a/app/models/ask.rb b/app/models/ask.rb index 63b3003..528cac3 100644 --- a/app/models/ask.rb +++ b/app/models/ask.rb @@ -18,6 +18,7 @@ class Ask field :last_updated_at, :type => DateTime index :topics + index :title # 提问人 belongs_to :user, :inverse_of => :asks diff --git a/app/models/topic.rb b/app/models/topic.rb index 334f881..f7ac895 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -42,4 +42,9 @@ def self.save_topics(topics, current_user_id) def self.find_by_name(name) find(:first,:conditions => {:name => name}) end + + def self.search_name(name, options = {}) + limit = options[:limit] || 10 + where(:name => /#{name}/i ).desc(:asks_count).limit(limit) + end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e867fc9..99d623b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,4 +1,4 @@ -<%= spaceless do %> +<% spaceless do %> @@ -43,7 +43,7 @@ cachedItems = searchCache.get(t); if(cachedItems == null){ $.ajax({ - url : "<%= search_asks_path(:format => 'json') %>", + url : "/search.json", data : { w : t }, dataType : "json", success : function(res){ @@ -74,7 +74,7 @@ html += ''+item_title+''; html += ''; } - html += '
  • 关于“'+t+'”更多搜索结果...
  • '; + html += '
  • 关于“'+t+'”更多搜索结果...
  • '; } else{ html += '
  • 没有找到关于“'+t+'”的结果: 添加这个问题
  • '; @@ -101,9 +101,9 @@
    <% if current_user %> - <%= Setting.app_name %> + <%= image_tag("logo.png") %> <% else %> - <%= Setting.app_name %> + <%= image_tag("logo.png") %> <% end %>
    diff --git a/config/routes.rb b/config/routes.rb index 3324e76..e6d1068 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,11 +28,13 @@ end match "auth/:provider/callback", :to => "users#auth_callback" - - resources :asks do + resources :search do collection do - get "search" + get "topics" end + end + + resources :asks do member do get "spam" get "follow" @@ -44,6 +46,7 @@ get "update_topic" end end + resources :answers do member do get "vote" diff --git a/config/sphinx.yml.default b/config/sphinx.yml.default index 4b50e32..5e6fda9 100644 --- a/config/sphinx.yml.default +++ b/config/sphinx.yml.default @@ -1,5 +1,5 @@ default: &defaults - listen: localhost:3312 + listen: localhost:9312 charset_dictpath: "db/dict/" charset_type: "zh_cn.utf-8" diff --git a/public/images/logo.png b/public/images/logo.png new file mode 100644 index 0000000..6c60d87 Binary files /dev/null and b/public/images/logo.png differ diff --git a/public/stylesheets/front.css b/public/stylesheets/front.css index f616a45..7f48604 100644 --- a/public/stylesheets/front.css +++ b/public/stylesheets/front.css @@ -33,6 +33,7 @@ address, blockquote { #header #site_name { background-color:#a82400; color:#FFF; height:35px; width:68px; text-align:center; line-height:35px; float:left; font-size:20px; font-weight:bold; } #header #site_name a { color:#FFF; text-decoration:none; } +#header #site_name a img { border:0; } #header #add_ask { display:inline-block; background:#000; height:35px; line-height:35px; padding:0 6px; overflow:hidden; } #header #add_ask input { width:360px; font-size: 16px;height: 18px; line-height:18px; border:0; margin-top: 2px; } #header #add_ask a { color: #F0F0F0; font-weight: bold; font-size: 13px; padding: 10px 10px; text-align: center;} @@ -316,4 +317,4 @@ a.user img.normal { width:100px; height:100px; } #footer { margin-top: 50px; -} \ No newline at end of file +}