Image_tag .blank吗?-回形针- Ruby on rails

时间:2021-09-10 00:27:10

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?

我刚刚将paperclip安装到我的ruby on rails博客应用程序中。一切都好工作……太大了。我正在试图弄清楚如何告诉paperclip不要输出任何东西,如果没有记录在表中,这样我就没有破碎的图像链接在任何地方。我怎么做,在哪里做?

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

3 个解决方案

#1


10  

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

要查看是否有与post关联的文件,可以使用post.photo.file?

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

#2


1  

has_attached_file takes

has_attached_file需要

:default_style => :medium, and

:default_style = >:媒介,

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

:default_url = > ' /图片/ 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.

作为参数——如果你想显示某种默认的图像而不是完全消除图像标签,这是@Voyta的解决方案。

#3


0  

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

我从你的问题中得到的一点是,下面的代码可以帮助你。

<% div_for post do %>


<% end unless post.blank? %>

#1


10  

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

要查看是否有与post关联的文件,可以使用post.photo.file?

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

#2


1  

has_attached_file takes

has_attached_file需要

:default_style => :medium, and

:default_style = >:媒介,

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

:default_url = > ' /图片/ 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.

作为参数——如果你想显示某种默认的图像而不是完全消除图像标签,这是@Voyta的解决方案。

#3


0  

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

我从你的问题中得到的一点是,下面的代码可以帮助你。

<% div_for post do %>


<% end unless post.blank? %>