0
我有具有我試圖調用視圖/用戶時參考作爲additional_column的接合柱的用戶模型/ _showcase.html.erb局部導軌3正確地使用動態列
有關完整的背景故事請參閱Rails 3 noob questions using partials correctly,因爲我被要求將問題分成兩個帖子。
的意見/用戶/ _showcase.html.erb
<td><img src="<%= user.avatar_url(32) %>" /></td>
<td><%= user.username%></td>
<% additional_columns.each do |column| %>
<td><%= user[column]%></td>
<% end %>
這裏是我在我的用戶模型來實現計算列
class User < ActiveRecord::Base
include ActionView::Helpers::DateHelper
... snip ...
# Get the last 10 users who made Karmunity accounts
scope :get_newest_users , order("created_at desc").limit(10);
# Get 10 users with the highest karma score
scope :get_highest_rated_user, order("karma_score desc").limit(10);
scope :avatar_url
scope :joined
# get the dated they joined in time ago format
def joined
time_ago_in_words(self.created_at)
end
# Get the gravatar image url
def avatar_url (size=nil)
query_string = '';
unless size.nil?
query_string = 's=' + size.to_s
end
"http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(self.email.strip.downcase) + "?#{query_string}"
end
end
然而,當我執行該代碼,user.avatar_url ,如果我手動鍵入user.joined它工作正常,但動態列(我假設它的行爲像用戶[:加入])返回一個空字符串。我敢肯定,我在這裏做了一些愚蠢的事情,但我不知道如何動態獲取計算/格式化的模型值。
爲了完整起見,這是我的電話到部分
<%= render :partial => 'users/showcase', :locals => { :id => 'newest_users', :title => 'Newest Users', :users => @newest_users, :additional_columns => [:affiliation,:joined]} %>
我會很感激的任何方向這是我第一次的Rails應用程序,我真的不知道我應該是什麼樣的問題問。
謝謝!