2012-04-04 47 views
0

我可以引入公司關聯的事件數據,但無法引入與該事件關聯的照片。Rails Showing Has_Many Linked Objects Photos

公司的has_many活動& 事件的has_many照片

目前我在公司/顯示的代碼看起來像

<% if @company.events.exists? then %> 
<% @company.events.offset(1).each do |event| %> 
<li><h3><%= link_to "#{event.name}", {:controller => "events", :action => "show", :id => event.id}, :rel => "external" %></h3> 
<p>By <%= event.artist %></p></li> 
<%= image_tag event.photo.photo.url(:large) %> 
<% end %> 
<% else %> 
    <p> There are no current events attached</p> 
<% end %> 

公司負責

def show 
    @company = Company.find(params[:id]) 
end 

我使用回形針的我的圖片。任何幫助將非常感激。

代碼形式事件模型(請記住我試圖從公司獲得)

class Event < ActiveRecord::Base 
    attr_accessible :name, :description, :artist, :starttime, :startdate, :finishdate, :company_id, :photos_attributes 
    belongs_to :company 
    has_many :photos 
    accepts_nested_attributes_for :photos, :allow_destroy => true, :reject_if => lambda { |a| a[:photo].blank? } 
    accepts_nested_attributes_for :company 

    end 
+0

目前,我有 @event = Event.where(:COMPANY_ID =>:ID,:爲了=> 'DESC') – 2012-04-04 03:54:02

+0

目前,我有我的CONTROLER 「@event = Event.where (:COMPANY_ID =>(PARAMS [:ID]))」 當我打電話這在我的顯示動作 '<%= IMAGE_TAG @event%>' 結果是空白圖像,這表明在HTML '#<activerecord::relation:0x102eca560>' – 2012-04-04 04:01:20

+0

您尚未正確設置回形針與您r事件模型。粘貼該關聯的代碼並編輯您的問題。 – 2012-04-04 05:26:51

回答

0

你缺少你的

has_attached_file :photo, :styles => {...} 

部分Event模型。

請參閱此Simple Example of Paperclip,如果您仍然遇到任何問題,請在此處發帖。

+0

嗨Jatin,感謝您的回覆,不幸的是它有點複雜,然後,照片有它自己的模型,因爲它處理大量的圖像屬於事件,has_attached_file存儲在照片模型中,在事件。公司也有在公司工作很好的圖像。它只是從事件數據庫中獲得第一張圖像,這是一個痛苦。你還有其他建議嗎? – 2012-04-04 10:04:38

+0

我現在可以看到它相當複雜。所以,根據你所說的,事件和公司的照片(間接圖像)是正確訪問的。只有當您訪問公司 - >活動 - >照片 - >圖片時,它纔會失敗。 – 2012-04-09 06:07:26

+0

它只是第一個無法訪問的圖像,或者它們都是?試試你的手在控制檯上,看看上面提到的鏈中的每個關聯是否正確。目前我只能幫你這麼多。快樂調試;) – 2012-04-09 06:09:53