Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 2.42 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Adding photos to blog comments using paperclip (nesting models?)
  2. class Post < ActiveRecord::Base
  3.  
  4.       validates :name,  :presence => true
  5.       validates :title, :presence => true,
  6.                         :length => { :minimum => 5 }
  7.  
  8.       has_many :comments, :dependent => :destroy
  9.       accepts_nested_attributes_for :comments
  10.  
  11.  
  12.      # Paperclip
  13.  
  14.       has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
  15.                         :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
  16.                         :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension"
  17.  
  18.       validates_attachment_presence :photo
  19.       validates_attachment_size :photo, :less_than => 5.megabytes
  20.       validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
  21.     end
  22.        
  23. class Comment < ActiveRecord::Base
  24.   attr_accessor :photo_file_name, :photo_file_size, :photo_content_type
  25.  
  26.   belongs_to :post
  27.  
  28.   # Paperclip
  29.  
  30.     has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
  31.                       :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
  32.                       :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension",
  33.                       :default_url => '/images/missing_:style.png'
  34.  
  35.  
  36.     validates_attachment_presence :photo
  37.     validates_attachment_size :photo, :less_than => 5.megabytes
  38.     validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
  39. end
  40.        
  41. <p id="notice"><%= notice %></p>
  42.  
  43. <p>
  44.  <% if @post.photo? %>
  45.    <%= image_tag @post.photo.url(:small) %>
  46.  <% else %>
  47.    No attachment available!
  48.  <% end %>
  49. </p>
  50. <p>
  51.   <b>Name:</b>
  52.   <%= @post.name %>
  53. </p>
  54.  
  55. <p>
  56.   <b>Title:</b>
  57.   <%= @post.title %>
  58. </p>
  59.  
  60. <p>
  61.   <b>Content:</b>
  62.   <%= @post.content %>
  63. </p>
  64.  
  65. <h2>Comments</h2>
  66.  <%= render @post.comments %>
  67.  
  68. <h2>Add a comment:</h2>
  69. <%= render "comments/form" %>
  70.  
  71. <br />
  72.  
  73. <%= link_to 'Edit', edit_post_path(@post) %> |
  74. <%= link_to 'Back', posts_path %> |
  75.        
  76. <p>
  77.  <%= image_tag comment.photo.url(:small) %>
  78. </p>
  79. <p>
  80.  <b>Commenter:</b>
  81.  <%= comment.commenter %>
  82. </p>
  83.  
  84. <p>
  85.  <b>Comment:</b>
  86.  <%= comment.body %>
  87. </p>
  88.  
  89. <p>
  90.  <%= link_to 'Destroy Comment', [comment.post, comment],
  91.                   :confirm => "Are you sure?",
  92.                  :method => :delete %>
  93. </p>
  94.        
  95. class Photo < ActiveRecord::Base
  96.   belongs_to :post
  97.   has_attached_file :data  
  98. end