[英]Image_tag .blank? - paperclip - Ruby on rails
我剛剛在我的ruby on rails博客應用程序中安裝了paperclip 。 一切都很好......太棒了。 我試圖弄清楚如果表中沒有記錄,如何告訴paperclip不要輸出任何內容,以便我沒有到處都有破碎的圖像鏈接。 我是怎么做到的?
這是我的代碼:
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 %>
要查看,如果有與post關聯的文件,您可以使用post.photo.file?
<%= image_tag post.photo.url(:small) if post.photo.file? %>
has_attached_file
需要
:default_style => :medium
,和
:default_url => '/images/default_image.png'
作為參數 - 如果你想顯示某種默認圖像而不是完全取消圖像標簽la @ Voyta的解決方案。
從小我得到你的問題可能下面的代碼將幫助你。
<% div_for post do %>
<% end unless post.blank? %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.