- Adding photos to blog comments using paperclip (nesting models?)
- class Post < ActiveRecord::Base
- validates :name, :presence => true
- validates :title, :presence => true,
- :length => { :minimum => 5 }
- has_many :comments, :dependent => :destroy
- accepts_nested_attributes_for :comments
- # Paperclip
- has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
- :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
- :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension"
- validates_attachment_presence :photo
- validates_attachment_size :photo, :less_than => 5.megabytes
- validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
- end
- class Comment < ActiveRecord::Base
- attr_accessor :photo_file_name, :photo_file_size, :photo_content_type
- belongs_to :post
- # Paperclip
- has_attached_file :photo, :styles => { :small => "150x150>", :large => "360x360" },
- :url => "/system/:class/:attachment/:id/:style_:basename.:extension",
- :path => ":rails_root/public/system/:class/:attachment/:id/:style_:basename.:extension",
- :default_url => '/images/missing_:style.png'
- validates_attachment_presence :photo
- validates_attachment_size :photo, :less_than => 5.megabytes
- validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
- end
- <p id="notice"><%= notice %></p>
- <p>
- <% if @post.photo? %>
- <%= image_tag @post.photo.url(:small) %>
- <% else %>
- No attachment available!
- <% end %>
- </p>
- <p>
- <b>Name:</b>
- <%= @post.name %>
- </p>
- <p>
- <b>Title:</b>
- <%= @post.title %>
- </p>
- <p>
- <b>Content:</b>
- <%= @post.content %>
- </p>
- <h2>Comments</h2>
- <%= render @post.comments %>
- <h2>Add a comment:</h2>
- <%= render "comments/form" %>
- <br />
- <%= link_to 'Edit', edit_post_path(@post) %> |
- <%= link_to 'Back', posts_path %> |
- <p>
- <%= image_tag comment.photo.url(:small) %>
- </p>
- <p>
- <b>Commenter:</b>
- <%= comment.commenter %>
- </p>
- <p>
- <b>Comment:</b>
- <%= comment.body %>
- </p>
- <p>
- <%= link_to 'Destroy Comment', [comment.post, comment],
- :confirm => "Are you sure?",
- :method => :delete %>
- </p>
- class Photo < ActiveRecord::Base
- belongs_to :post
- has_attached_file :data
- end