2013-01-05 105 views
1

我正在嘗試創建鏈接到相冊的圖庫頁面。相冊工作正常,但我試圖將每個gallery_id的第一個圖像拉到畫廊頁面。我有一個畫廊有很多照片,並且照片屬於畫廊。我得到的是爲每張專輯加載的第一張圖片。創建圖庫頁面/嵌套資源

類GalleriesController < ApplicationController中

def index 
@gallery = Gallery.paginate(page: params[:page]).per_page(6) 
@photos = Photo.find(:all, :limit => 1) 

end 

DEF顯示 @gallery = Gallery.find(PARAMS [:ID]) @照片= @ gallery.photos.all 端 端

畫廊/index.html。

<% provide(:title, 'Photo Galleries') %>. 

<div id="galleries"> 
    <%= will_paginate @gallery %> 
    <ul class="thumbnails"> 
     <% @gallery.each do |gallery| %> 
      <li class="span4"> 
       <div class="thumbnail"> 
        <% @photos.each do |photo| %> 
         <%= link_to image_tag(photo.image), gallery_path(gallery)%> 
        <% end %> 
        <h4><%= gallery.name %></h4> 
       </div> 
      </li> 
     <% end %> 
    </ul> 
</div> 

路線

resources :galleries, :has_many => :photos 

任何幫助,將不勝感激。

+0

塞斯,看起來你是新來的。歡迎。你的問題有一些格式問題,我不太清楚你想完成什麼。什麼是模型及其關係?你說一個畫廊has_many照片。但你也提到了專輯。相冊在哪裏發揮作用? – johnnycakes

+0

你說:「我正在嘗試將每個gallery_id中的第一個圖像拉到畫廊頁面」。然後你說:「我得到的是每張專輯的第一張圖片加載。」對我來說,它就像你得到你想要的一樣。請澄清。謝謝。 – johnnycakes

+0

對不起,我不是指專輯,意思是畫廊。發生了什麼事情,我得到了photo_id 1作爲每張專輯的封面。 – sethb78

回答

0

你必須使用關係,這是你在這裏需要的。我試圖修復代碼並添加評論。

class GalleriesController < ApplicationController 
def index 
    @gallery = Gallery.paginate(page: params[:page]).per_page(6) 
    # You don't need the photos. You have to access them through gallery, 
    # or you will get always all photos independent of the gallery. 
    #@photos = Photo.find(:all, :limit => 1) 
end 

這是你正在尋找

# galleries/index.html.erb 
<% provide(:title, 'Photo Galleries') %>. 

<div id="galleries"> 
    <%= will_paginate @gallery %> 
    <ul class="thumbnails"> 
     <% @gallery.each do |gallery| %> 
      <li class="span4"> 
       <div class="thumbnail"> 
        <% gallery.photos.each do |photo| %> 
         <%= link_to image_tag(photo.image), gallery_path(gallery)%> 
        <% end %> 
        <h4><%= gallery.name %></h4> 
       </div> 
      </li> 
     <% end %> 
    </ul> 
</div> 

如果你只是想顯示每一個畫廊的第一張照片的看法,你必須改變的觀點是這樣的:

# galleries/index.html.erb 
<% provide(:title, 'Photo Galleries') %>. 

<div id="galleries"> 
    <%= will_paginate @gallery %> 
    <ul class="thumbnails"> 
     <% @gallery.each do |gallery| %> 
      <li class="span4"> 
       <div class="thumbnail"> 
        <%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery)%> 
        <h4><%= gallery.name %></h4> 
       </div> 
      </li> 
     <% end %> 
    </ul> 
</div> 
+0

但是,此代碼輸出圖庫索引中圖庫的所有照片。我想我需要找到一種方法來說,找到第一個圖像,其中photo.gallery_id == gallery_id ???? – sethb78

+0

請將問題標記爲已解決,如果這是答案 – Fa11enAngel

1

很肯定這是你想要什麼:

class GalleriesController < ApplicationController 
    def index 
     @gallery = Gallery.paginate(page: params[:page]).per_page(6) 
    end 
end 

_

# galleries/index.html 

<% provide(:title, 'Photo Galleries') %> 

<div id="galleries"> 
    <%= will_paginate @gallery %> 
    <ul class="thumbnails"> 
     <% @gallery.each do |gallery| %> 
      <li class="span4"> 
       <div class="thumbnail"> 
         <%= link_to image_tag(gallery.photos.first.image), gallery_path(gallery) %> 
        <h4><%= gallery.name %></h4> 
       </div> 
      </li> 
     <% end %> 
    </ul> 
</div> 

在你的問題是,在你的索引行動你抓着的所有圖像,不管他們屬於哪個畫廊,然後你通過所有這些在你看來循環並顯示它們。

由於您的關聯(gallery has_many照片),您可以使用gallery.photos訪問圖庫的照片。

在我的例子,我在顯示第一圖像達每家畫廊:gallery.photos.first

如果你想從有問題,你可以使用sample圖庫隨機圖像。即gallery.photos.sample