2010-04-26 97 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解决方案中的图片标签。

相关问题