Skip to content

Commit

Permalink
Add: Logo改为图片;
Browse files Browse the repository at this point in the history
Mod: 搜索调整到 SearchController;
Add: Topic搜索的功能,但页面还未实现;
  • Loading branch information
huacnlee committed Apr 3, 2011
1 parent d7354e3 commit 5007a8f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 10 deletions.
28 changes: 28 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/models/ask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Ask
field :last_updated_at, :type => DateTime

index :topics
index :title

# 提问人
belongs_to :user, :inverse_of => :asks
Expand Down
5 changes: 5 additions & 0 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= spaceless do %>
<% spaceless do %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -74,7 +74,7 @@
html += '<a href="/asks/'+res[i]._id+'">'+item_title+'</a>';
html += '</li>';
}
html += '<li class="more" onclick="location.href=\'<%= search_asks_path %>?w='+t+'\';">关于“'+t+'”更多搜索结果...</li>';
html += '<li class="more" onclick="location.href=\'/search?w='+t+'\';">关于“'+t+'”更多搜索结果...</li>';
}
else{
html += '<li>没有找到关于“'+t+'”的结果: <a href="#" onclick="return addAsk();">添加这个问题</a></li>';
Expand All @@ -101,9 +101,9 @@
<div class="left_wrapper">
<div id="site_name">
<% if current_user %>
<a href="/"><%= Setting.app_name %></a>
<a href="/"><%= image_tag("logo.png") %></a>
<% else %>
<a href="/asks"><%= Setting.app_name %></a>
<a href="/asks"><%= image_tag("logo.png") %></a>
<% end %>
</div>
<div id="add_ask">
Expand Down
9 changes: 6 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -44,6 +46,7 @@
get "update_topic"
end
end

resources :answers do
member do
get "vote"
Expand Down
2 changes: 1 addition & 1 deletion config/sphinx.yml.default
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default: &defaults
listen: localhost:3312
listen: localhost:9312
charset_dictpath: "db/dict/"
charset_type: "zh_cn.utf-8"

Expand Down
Binary file added public/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion public/stylesheets/front.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;}
Expand Down Expand Up @@ -316,4 +317,4 @@ a.user img.normal { width:100px; height:100px; }

#footer {
margin-top: 50px;
}
}

0 comments on commit 5007a8f

Please sign in to comment.