photo.rb
class Photo < ActiveRecord::Base
belongs_to :post, :class_name => "Forem::Post"
has_attached_file :attachment, :styles => {
:medium => "300x300>", :thumb => "100x100>"
}
validates_attachment_presence :attachment
end
decorators/controllers/forem/topics_controller_decorator.rb
Forem::TopicsController.class_eval do
def new
authorize! :create_topic, @forum
@topic = @forum.topics.build
post = @topic.posts.build
post.photos.build
end
end
decorators/controllers/forem/posts_controller_decorator.rb
Forem::PostsController.class_eval do
def create
@post = @topic.posts.new(post_params)
@post.user = forem_user
end
protected
def post_params
params.require(:post).permit(:text, :password, :email, :photos_attributes)
end
end
decorators/models/forem/posts_decorator.rb
Forem::Post.class_eval do
has_many :photos
accepts_nested_attributes_for :photos
end
app/views/forem/posts/_post.html.erb
<% for photo in post.photos %>
<%= image_tag photo.attachment.url(:medium) %>
<% end %>
app/views/forem/posts/_form.html.erb
<%= f.simple_fields_for :photos do |photo| %>
<%= render :partial => "photos/form", :locals => { :f => photo }%>
<% end %>
app/views/photos/_form.html.erb
<%= f.file_field :attachment, :id => "photo_attachment" %>
20141017153109_create_photos.rb
class CreatePhotos < ActiveRecord::Migration
def change
create_table :photos do |t|
t.integer :post_id
t.timestamps
end
end
end
20141017153223_add_attachment_columns_to_photos.rb
class AddAttachmentColumnsToPhotos < ActiveRecord::Migration
def self.up
change_table :photos do |t|
t.has_attached_file :attachment
end
end
def self.down
drop_attached_file :photos, :attachment
end
end