From ce6d63ccafb6b970abdb5079334150a31571bb11 Mon Sep 17 00:00:00 2001 From: alan <547533434@qq.com> Date: Mon, 14 Mar 2016 10:24:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84user.find?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/user.rb | 11 +++ .../welcome/_search_all_results.html.erb | 80 ++++++++----------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index a74c20751..259b9d04e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1064,6 +1064,17 @@ class User < Principal anonymous_user end + # refactor User model find function, + # return anonymous user when can not find user id = user_id + def self.find user_id + user = User.new + begin + user = super + rescue + user = self.anonymous + end + user + end # Salts all existing unsalted passwords # It changes password storage scheme from SHA1(password) to SHA1(salt + SHA1(password)) # This method is used in the SaltPasswords migration and is to be kept as is diff --git a/app/views/welcome/_search_all_results.html.erb b/app/views/welcome/_search_all_results.html.erb index 51835106b..a714f53d2 100644 --- a/app/views/welcome/_search_all_results.html.erb +++ b/app/views/welcome/_search_all_results.html.erb @@ -1,31 +1,31 @@ <% unless all_results.nil? || all_results.empty?%> -<% all_results.each do |item|%> - <% case item.type %> - <% when 'user'%> - - <% when 'attachment'%> + <% when 'attachment'%> - <% when 'project'%> + <% when 'project'%>
@@ -103,14 +92,13 @@
  • <%= item.try(:highlight).try(:content) ? item.highlight.content[0].html_safe : item.content.html_safe%>
  • - <%= memo_author = User.where("id = ?", item.author_id).first if item.author_id? %> -
  • 发帖人:<%= memo_author.nil? ? '无' : memo_author.login %>更新时间:<%= format_date( item.updated_at) %>
  • +
  • 发帖人:<%= item.author_id ? User.find(item.author_id).login : '无' %>更新时间:<%= format_date( item.updated_at) %>
  • - <%end %> -<% end %> + <%end %> + <% end %>
    <%= paginate all_results,:params => {:controller => 'welcome', :action => 'search',:search_type=>'all'}%>