2015-04-05 18 views
1

我正在爲朋友建立一個攝影網站,她希望能夠上傳,編輯和刪除圖像。 在localhost上一切正常,但我注意到,當我編輯圖像時,它改變位置併成爲最後一個。有誰知道爲什麼發生這種情況,或者可以告訴我如何從創建日期後對圖像進行排序?Rails:更新後的圖像更改位置

index.html.erb:

<section class='container'> 
    <article id='gallery'> 
    <% PreviewPicture.all.each do |preview_picture| %> 
     <a class="fancybox" rel="group" href="<%= preview_picture.picture.image.url(:original)%>" ><%= image_tag preview_picture.image.url(:original), :class => 'grayscale' %></a> 
     <% if user_signed_in? %> 
      <%= link_to "Delete #{preview_picture.id}", preview_picture_path(:id => preview_picture.id), method: :delete %> 
      <%= link_to "Edit #{preview_picture.id}", edit_preview_picture_path(:id => preview_picture.id) %> 
     <% end %> 
    <% end %> 
    </article> 
</section> 

<% if user_signed_in? %> 
    <%= link_to "Add Picture", new_preview_picture_path, :class => "add_preview_picture" %> 
<% end %> 

gallery.css:

section.container { 
    margin-top: 100px; 
    text-align: center; 
    width: 69%; 
} 

img.grayscale{ 
    margin: 1.5px 0px; 
    filter: grayscale(100%); 
    -webkit-filter: grayscale(100%); /* For Webkit browsers */ 
    filter: gray; /* For IE 6 - 9 */ 
} 

img.grayscale:hover{ 
    filter: grayscale(0%); 
    -webkit-filter: grayscale(0%); 
    filter: none; 
} 

回答

0

您將要創建的PreviewPicture模型,能對一個範圍或一個類的方法按日期創建或更新的圖片。

class PreviewPicture < ActiveRecord::Base 
    def self.by_date 
     order(created_at: :desc) // or order(updated_at: :desc) 
    end 
end 

然後你可以使用它像這樣:

PreviewPicture.by_date.each 

這是很好的做法是將這個控制器,所以在您看來的相應PreviewPictureController行動:

@preview_pictures = PreviewPicture.by_date 

然後遍歷通過您視圖中的@preview_pictures實例變量。

+0

你能解釋downvote嗎? – phillyslick 2015-04-05 19:36:39

+1

這不是我把downvote ..我剛剛看到你的答案!它看起來不錯,我會試着知道。非常感謝你:) – ellen 2015-04-06 16:21:51

+1

謝謝!那解決了我的問題:) – ellen 2015-04-06 16:46:48