2016-12-12 23 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> 
相关问题