2010-04-26 98 views
2

我剛剛在我的ruby on rails博客應用程序中安裝了paperclip。一切都很好,太棒了。我想弄清楚如何在表格中沒有記錄的情況下讓回形針不輸出任何內容,這樣我就沒有破碎的圖像鏈接。我該怎麼做,在哪裏做?Image_tag .blank? - 回形針 - Ruby on rails

這裏是我的代碼:

class Post < ActiveRecord::Base 

    has_attached_file :photo, :styles => { :small => "150x150"} 
    validates_presence_of :body, :title 
    has_many :comments, :dependent => :destroy 
    has_many :tags, :dependent => :destroy 
    has_many :ugtags, :dependent => :destroy 
    has_many :votes, :dependent => :destroy 
    belongs_to :user 
    after_create :self_vote 
     def self_vote 
     # I am assuming you have a user_id field in `posts` and `votes` table. 
     self.votes.create(:user => self.user) 
     end 

    cattr_reader :per_page 
    @@per_page = 10 

end 

查看

<% div_for post do %> 
    <div id="post-wrapper"> 
     <div id="post-photo"> 
      <%= image_tag post.photo.url(:small) %> 
      </div> 
    <h2><%= link_to_unless_current h(post.title), post %></h2> 
    <div class="light-color"> 
    <i>Posted <%= time_ago_in_words(post.created_at) %></i> ago 
    </div> 
    <%= simple_format truncate(post.body, :length => 600) %> 
    <div id="post-options"> 
    <%= link_to "Read More >>", post %> | 
    <%= link_to "Comments (#{post.comments.count})", post %> | 
    <%= link_to "Strings (#{post.tags.count})", post %> | 
    <%= link_to "Contributions (#{post.ugtags.count})", post %> | 
    <%= link_to "Likes (#{post.votes.count})", post %> 
    </div> 
    </div> 


<% end %> 

回答

10

看看,是否有與職務相關的文件,你可以使用post.photo.file?

<%= image_tag post.photo.url(:small) if post.photo.file? %> 
+0

noob問題,是'檔案?'一般的方法或回形針給出的方法 – carbonr 2012-11-26 06:46:29

0

從小事我得到烏爾問題可能下面的代碼將幫助你。

<% div_for post do %> 


<% end unless post.blank? %> 
+0

這似乎並不奏效。我只是想告訴系統我不希望它顯示與照片相關的照片,除非實際上有照片關聯。除非post.blank?看起來像我想要做的,但只是在特定的列。上面的代碼似乎沒有改變任何東西。沒有破壞任何東西,但沒有改變任何東西。讓我知道是否需要提供更多信息。 – bgadoci 2010-04-26 05:42:41

1

has_attached_file需要

:default_style => :medium

:default_url => '/images/default_image.png'

作爲參數以及 - 如果喲你想展示某種默認圖片,而不是完全消除la @ Voyta解決方案中的圖片標籤。

相關問題