2010-05-25 85 views
0

好吧,我正在嘗試顯示用戶的個人資料圖片。我設置的應用程序允許用戶創建問題和答案(我在代碼中調用答案的網站)我試圖這樣做的視圖在/views/questions/show.html.erb文件中。也可能會注意到我正在使用Paperclip寶石。這裏是設置:通過問題的答案爲用戶顯示圖片

協會

Users 

class User < ActiveRecord::Base 

    has_many :questions, :dependent => :destroy 
    has_many :sites, :dependent => :destroy 
    has_many :notes, :dependent => :destroy 
    has_many :likes, :through => :sites , :dependent => :destroy 
    has_many :pics, :dependent => :destroy 
    has_many :likes, :dependent => :destroy 


end 

問題

class Question < ActiveRecord::Base 
    has_many :sites, :dependent => :destroy 
    has_many :notes, :dependent => :destroy 
    has_many :likes, :dependent => :destroy 
    belongs_to :user 
end 

答案(網站)

class Site < ActiveRecord::Base 

    belongs_to :question 
    belongs_to :user 
    has_many :notes, :dependent => :destroy 
    has_many :likes, :dependent => :destroy 

    has_attached_file :photo, :styles => { :small => "250x250>" } 
end 

照片管理

class Pic < ActiveRecord::Base 
    has_attached_file :profile_pic, :styles => { :small => "100x100" } 
    belongs_to :user 
end 

/views/questions/show.html.erb被渲染局部/views/sites/_site.html.erb被調用應答(點)有:

<% div_for site do %> 
<%=h site.description %> 
<% end %> 

我一直在努力做的事情一樣:

<%=image_tag site.user.pic.profile_pic.url(:small) %> 
<%=image_tag site.user.profile_pic.url(:small) %> 

等,但是,這顯然是錯誤的。我的錯誤指向我的問題#show動作,所以我想象我需要在那裏定義一些東西,但不知道是什麼。是否有可能調用圖片給出當前的關聯,調用的位置,如果有的話,我需要做什麼控制器添加,以及哪些代碼行將調用圖片?

更新:這裏是QuestionsController#顯示代碼:

def show 

    @question = Question.find(params[:id]) 
    @sites = @question.sites.all(:select => "sites.*, SUM(likes.like) as like_total", 
      :joins => "LEFT JOIN likes AS likes ON likes.site_id = sites.id", 
      :group => "sites.id", 
      :order => "like_total DESC") 

    respond_to do |format| 
     format.html # show.html.erb 
     format.xml { render :xml => @question } 
    end 
    end 

回答

0

你的圖片has_many協會:

class User < ActiveRecord::Base 
    ... 
    has_many :pics, :dependent => :destroy 
end 

但是你想採取行動,因爲如果只有一個PIC :

<%=image_tag site.user.pic.profile_pic.url(:small) %> 

所以要麼拍第一張照片(可能你還應該檢查它是否e xists):

<%=image_tag site.user.pics.first.profile_pic.url(:small) %> 

或改變關聯has_one如果用戶只能有一個畫面:

class User < ActiveRecord::Base 
    ... 
    has_one :pic, :dependent => :destroy 
end 

<%=image_tag site.user.pic.profile_pic.url(:small) %> 
+0

那完美。謝謝。 – bgadoci 2010-05-25 15:36:51