views:

197

answers:

3

I have just installed paperclip into my ruby on rails blog application. Everything is working great...too great. I am trying to figure out how to tell paperclip not to output anything if there is no record in the table so that I don't have broken image links everywhere. How, and where, do I do this?

Here is my code:

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

View

<% 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 %>
A: 

From the little i get ur question may following code will help you.

<% div_for post do %>


<% end unless post.blank? %>
Salil
That doesn't seem to work. I am just trying to tell the system that I do not want it to display a :photo that is associated with a post unless there is actually photo associated. The unless post.blank? seems like what I am trying to do but just on the specific column. The code above didn't seem to change anything. Didn't break anything but didn't change anything. Let me know if I need to give more information.
bgadoci
+3  A: 

To see, if there is a file associated with post, you can use post.photo.file?

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

has_attached_file takes

:default_style => :medium, and

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

as arguments as well - if you wanted to show some kind of default image rather than eliminating the image tag entirely a la @Voyta's solution.

Tim Snowhite