好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

paperclip check is have attachment

paperclip check is have attachment

Rails Plugin: Paperclip Question

up vote 5 down vote favorite

2

How can I prevent the image tag that calls the associated image from displaying if no image is associated with the record?

  <%=   image_tag   @agent  .  avatar  .  url %>

...gives me the text "Missing" if there is no image associated with that agent. I want to test to see there is an image available first, then render the above tag if the test returns true.

Better yet, is there anyway for me to specify a default image if no image is specifically provided?

link | flag

asked  Nov 17 '08 at 21:13

neezer
1,340 1 14 30


add comment

start a bounty

3 Answers

oldest newest votes

up vote 5 down vote accepted

I use the following to find wether a model has an associated attachment:

  <%     if     @agent  .  avatar  .  file  ?   %>
  <%= image_tag @agent . avatar . url (: normal ) %>
<% else %>
  No attachment available!
<% end %>

link | flag

answered  Nov 17 '08 at 23:22

Christoph Schiessl
1,691 4 18

add comment

up vote 9 down vote

Okay, so I got one part of it.

Specifying a default image happens in the model

  has_attached_file   :  avatar  ,     :  default_url   =>     '/images/brokers/agents/anonymous_icon.jpg'  

link | flag

answered  Nov 17 '08 at 21:24

neezer
1,340 1 14 30

add comment

up vote 7 down vote

Few bytes less:

  <%     if     @agent  .  avatar  ?   %>
  <%= image_tag @agent . avatar . url (: normal ) %>
<% else %>
  No attachment available!
<% end %>

link | flag

answered  Mar 2 at 14:15

tig
1,046   3   15

 

   

Give this man a bickie. –  Smickie   Apr 19 at 9:14

http://stackoverflow.com/questions/296952/rails-plugin-paperclip-question

查看更多关于paperclip check is have attachment的详细内容...

  阅读:33次