Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save frankie-loves-jesus/c40400731b074cd249b6 to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/c40400731b074cd249b6 to your computer and use it in GitHub Desktop.
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
class CreatePhotos < ActiveRecord::Migration
def change
create_table :photos do |t|
t.integer :post_id
t.timestamps
end
end
end
<%= f.simple_fields_for :photos do |photo| %>
<%= f.file_field :attachment, :id => "photo_attachment" %>
<% end %>
<div class="photos">
<% for photo in post.photos %>
<%= image_tag photo.attachment.url(:medium) %>
<% end %>
</div>
class Photo < ActiveRecord::Base
belongs_to :post, :class_name => "Forem::Post"
has_attached_file :attachment,
path: ":rails_root/public/system/attachments/:id/:style/:filename",
url: "/system/attachments/:id/:style/:filename",
styles: {
display: "480x480>",
thumbnail: "70x70#"
},
convert_options: { all: "-quality 100" },
processors: [:thumbnail, :compression]
validates_attachment_presence :attachment
validates_attachment :attachment, content_type: {
content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]
}
end
Forem::Post.class_eval do
has_many :photos
accepts_nested_attributes_for :photos
end
Forem::PostsController.class_eval do
protected
def post_params
params.require(:post).permit(:text, :password, :email, photos_attributes: [])
end
end
Forem::TopicsController.class_eval do
def new
authorize! :create_topic, @forum
@topic = @forum.topics.build
post = @topic.posts.build
post.photos.build
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment