Skip to content

Instantly share code, notes, and snippets.

@frankie-loves-jesus
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-loves-jesus/f25e546844d9771e7d7c to your computer and use it in GitHub Desktop.
Save frankie-loves-jesus/f25e546844d9771e7d7c to your computer and use it in GitHub Desktop.
Forem with Paperclip

Forem with Paperclip

Live app:

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 
Started POST "/default/topics" at 2014-10-17 17:35:28 +0000
Processing by Forem::TopicsController#create as HTML
Parameters: {"utf8"=>"?", "authenticity_token"=>"rCLEcuRvkWU8Jsr+cEjtC1tTF97JGx/9ii54Gw9H42E=", "topic"=>{"subject"=>"asdasdasda", "posts_attributes"=>{"0"=>{"text"=>"dasdasdasd", "photos_attributes"=>{"0"=>{"attachment"=>#<ActionDispatch::Http::UploadedFile:0x0000000490f048 @tempfile=#<File:/tmp/RackMultipart20141017-634-1by49l4>, @original_filename="download.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"topic[posts_attributes][0][photos_attributes][0][attachment]\"; filename=\"download.jpg\"\r\nContent-Type: image/jpeg\r\n">}}}}}, "commit"=>"Create Topic", "forum_id"=>"default"}
�[1m�[36mUser Load (0.3ms)�[0m �[1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1�[0m
�[1m�[35mForem::Forum Load (0.2ms)�[0m SELECT "forem_forums".* FROM "forem_forums" WHERE "forem_forums"."slug" = 'default' ORDER BY "forem_forums"."name" ASC LIMIT 1
�[1m�[36mForem::Category Load (0.1ms)�[0m �[1mSELECT "forem_categories".* FROM "forem_categories" WHERE "forem_categories"."id" = ? LIMIT 1�[0m [["id", 1]]
Unpermitted parameters: photos_attributes
�[1m�[35m (0.1ms)�[0m begin transaction
�[1m�[36mForem::Topic Exists (0.2ms)�[0m �[1mSELECT 1 AS one FROM "forem_topics" WHERE ("forem_topics"."id" IS NOT NULL) AND "forem_topics"."slug" = 'asdasdasda' LIMIT 1�[0m
�[1m�[35mSQL (0.4ms)�[0m INSERT INTO "forem_topics" ("created_at", "forum_id", "slug", "subject", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", "2014-10-17 17:35:28.968381"], ["forum_id", 1], ["slug", "asdasdasda"], ["subject", "asdasdasda"], ["updated_at", "2014-10-17 17:35:28.968381"], ["user_id", 1]]
�[1m�[36mSQL (0.2ms)�[0m �[1mINSERT INTO "forem_posts" ("created_at", "text", "topic_id", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?)�[0m [["created_at", "2014-10-17 17:35:28.970016"], ["text", "dasdasdasd"], ["topic_id", 15], ["updated_at", "2014-10-17 17:35:28.970016"], ["user_id", 1]]
�[1m�[35mForem::Topic Load (0.0ms)�[0m SELECT "forem_topics".* FROM "forem_topics" WHERE "forem_topics"."id" = ? LIMIT 1 [["id", 15]]
�[1m�[36mSQL (0.3ms)�[0m �[1mUPDATE "forem_topics" SET "last_post_at" = '2014-10-17 17:35:28.970016' WHERE "forem_topics"."id" = 15�[0m
�[1m�[35mForem::Subscription Load (0.1ms)�[0m SELECT "forem_subscriptions".* FROM "forem_subscriptions" WHERE "forem_subscriptions"."topic_id" = ? ORDER BY "forem_subscriptions"."id" ASC LIMIT 1000 [["topic_id", 15]]
�[1m�[36mSQL (0.1ms)�[0m �[1mUPDATE "forem_posts" SET "notified" = 't' WHERE "forem_posts"."id" = 15�[0m
�[1m�[35mSQL (0.1ms)�[0m UPDATE "forem_posts" SET "state" = 'approved' WHERE "forem_posts"."id" = 15
�[1m�[36m (0.1ms)�[0m �[1mSELECT COUNT(*) FROM "forem_subscriptions" WHERE "forem_subscriptions"."topic_id" = ? AND "forem_subscriptions"."subscriber_id" = 1�[0m [["topic_id", 15]]
�[1m�[35mSQL (0.1ms)�[0m INSERT INTO "forem_subscriptions" ("subscriber_id", "topic_id") VALUES (?, ?) [["subscriber_id", 1], ["topic_id", 15]]
�[1m�[36mSQL (0.1ms)�[0m �[1mUPDATE "forem_topics" SET "state" = 'approved' WHERE "forem_topics"."id" = 15�[0m
�[1m�[35m (8.0ms)�[0m commit transaction
Redirected to http://web-025a9cdf-c1cd-4f15-b7e7-54558afbdf6c.runnable.com/default/topics/asdasdasda
Completed 302 Found in 123ms (ActiveRecord: 10.7ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment