2016-12-12 25 views
0

我目前正在研究一個需要我創建(真實)房地產經紀人網站的項目。我導入了一個包含所有屬性的XML列表並創建了數據庫,但是當我創建一個屬性列表併爲每個屬性插入第一個圖像時,它會創建兩個圖像(列表中的每個屬性一個),並將這兩個圖像到每個屬性。只有一個需要輸出兩個圖像的導軌

的代碼是:

<% @properties.each do |property| %> 
     <div class="row"> 
     <div class="col-sm-12"> 

      <div class="row"> 
      <div class="col-sm-3"> 
       <% property.pictures.each do |picture| %> 
        <% if picture.name.eql?('Photo 10') %> 
         <img src="<%= picture.url %>" class="img-responsive center-block"/> 
        <% end %> 
       <% end %> 
      </div> 
      <div class="col-sm-9"> 
       <h4><%= property.advert_heading %></h4> 
       <p><%= property.main_advert %></p> 
      </div> 
      </div> 

     </div> 
     </div> 
    <% end %> 

的HTML輸出是:

<div class="row"> 
     <div class="col-sm-12"> 

      <div class="row"> 
      <div class="col-sm-3"> 
         <img href="http://med01.expertagent.co.uk/in4glestates/{376a3e5b-f940-4181-bc8e-255859c03e51}/{0b306ad6-63d3-4af2-a3ac-0dfa0885b724}/main/P1000507.jpg" class="img-responsive center-block"/> 
         <img href="http://med01.expertagent.co.uk/in4glestates/{376a3e5b-f940-4181-bc8e-255859c03e51}/{5004cf3b-e189-48e9-a1a6-f029e402ddd3}/main/P1000507.jpg" class="img-responsive center-block"/> 
      </div> 
      <div class="col-sm-9"> 
       <h4>Pen Y Bryn, Llanfairfechan</h4> 
       <p>A semi detached three bedroom family home located in a quiet cul de sac in the upper part of the village of Llanfairfechan. The property benefits from double glazed windows, gas central heating, gardens to front and rear. This would make an ideal family home or investment property.</p> 
      </div> 
      </div> 

     </div> 
     </div> 
     <div class="row"> 
     <div class="col-sm-12"> 

      <div class="row"> 
      <div class="col-sm-3"> 
         <img href="http://med01.expertagent.co.uk/in4glestates/{376a3e5b-f940-4181-bc8e-255859c03e51}/{0b306ad6-63d3-4af2-a3ac-0dfa0885b724}/main/P1000507.jpg" class="img-responsive center-block"/> 
         <img href="http://med01.expertagent.co.uk/in4glestates/{376a3e5b-f940-4181-bc8e-255859c03e51}/{5004cf3b-e189-48e9-a1a6-f029e402ddd3}/main/P1000507.jpg" class="img-responsive center-block"/> 
      </div> 
      <div class="col-sm-9"> 
       <h4>33, Pen Y Bryn, Llanfairfechan LL33 0UH</h4> 
       <p>A semi detached three bedroom family home located in a quiet cul-de-sac in the upper part of Llanfairfechan village. The property benefits from double glazed windows, gas central heating, gardens to front and rear. Restrictions apply. Application fees apply. Deposit: &amp;pound;750.</p> 
      </div> 
      </div> 

     </div> 
     </div> 

對我來說,似乎Rails是循環throught的property.picture.each兩次(或者我猜更多如果我有更多的屬性)並插入輸出兩次,但我不明白爲什麼。

非常感謝

+0

是圖片名稱獨特之處? – RSB

+2

圖片的URL是不同的,因此是不一樣的圖像,你不是迭代兩次。您已經存儲了兩個具有相同名稱但網址不同的圖像。 – tebayoso

+1

很確定property.pictures包含名稱爲「照片10」的多張照片。仔細檢查xml文件並解析它。 – luckyruby

回答

1

它看起來像你有一個名稱屬性爲'照片10'重複的圖片。使用您的導軌控制檯運行查詢來確認。

Picture.where(name: 'Photo 10').count 

返回多少條記錄?你的期望是每個房產都會有一張名爲「照片10」的照片?如果是這樣,你應該期望有多少圖片作爲屬性返回,如果不是,那麼你有重複的條目與名稱「照片10」爲每個屬性。這讓我想到了下一點。

您應該normalize your database。事實上,你依靠一個非唯一的屬性,並且有多個圖片條目指向相同的url告訴我。您還在圖片表中創建了大量不必要的圖片條目。相反,我會在屬性和圖片之間創建一個連接表,也許稱爲PropertyPictures。對於每個唯一圖片網址,請在圖片表中創建一個條目。對於每個使用圖片的屬性,使用該property_id和所需圖片的picture_id在連接表中創建一個條目。這將有助於防止您的照片改變。就像你現在所做的那樣,如果一張圖片的網址發生變化,即使它應該在所有地方都不會改變。就您的觀點而言,既然您擁有獨一無二且一致的picture_id,請使用它,這將不可能有重複。此外,而不是拉所有的照片,只是得到你想要的。你可以在你的財產模型中寫一個方法來做到這一點。這可能是這樣的:

def special_picture 
    pictures.where(id: 1) 
end 

而在你的看法:

<% @properties.each do |property| %> 
     <div class="row"> 
     <div class="col-sm-12"> 

      <div class="row"> 
      <div class="col-sm-3"> 
         <img src="<%= property.special_picture.url %>" class="img-responsive center-block"/> 
      </div> 
      <div class="col-sm-9"> 
       <h4><%= property.advert_heading %></h4> 
       <p><%= property.main_advert %></p> 
      </div> 
      </div> 

     </div> 
     </div> 
    <% end %> 

`

+0

謝謝你的答案,真的幫助清除它,我現在有一個屬性和圖片的連接表,它完美的作品。再次感謝 –

0

如果你只想要第一個圖像的每個上市,你爲什麼要通過所有循環每個列表的圖片?

你可以這樣做,而不是:

<div class="col-sm-3"> 
    <img src="<%= property.pictures.first.url %>" class="img-responsive center-block"/> 
</div> 
相關問題