0
votes

I am using RMagick gem to make a rails app that turns images into animated gifs. There is Post model, which has_many post_attachments and has_one post_output. The idea is users can upload multiple images and get redirected to a page with a resulting animated gif. I am having a problem with attaching the gif to post_output(which has an attribute called 'output') via carrierwave, AWS S3, and fog.

posts_controller.rb

def create
  @post = Post.new(post_params)

  if @post.save
    @post_attachments = params[:post_attachments][:image].map do |attachment|
      @post.post_attachments.create!(image: attachment)
    end

    images = @post_attachments.map { |attachment| attachment.image.url  }
    gif = Magick::ImageList.new(*images)
    gif.to_blob
    gif.delay = 20
    filename = "#{@post.title}.gif"
    gif.write(filename)

    binding.pry
    @post.post_output.create!(output: gif)


    flash.now[:success] = "Post was succesfully created!"
    redirect_to @post

  else
    flash.now[:danger] = "Something went wrong. Try again. :("
    render 'new'
  end
end

def post_params
    params.require(:post).permit(:title,
    post_attachments_attributes: [:id, :post_id, :image],
    post_output_attributes: [:id, :post_id, :output])
end

post.rb

class Post < ActiveRecord::Base
  has_many :post_attachments, dependent: :destroy
  has_one :post_output,     dependent: :destroy
  accepts_nested_attributes_for :post_attachments
  accepts_nested_attributes_for :post_output

  validates :title, presence: true
end  

post_output.rb

class PostOutput < ActiveRecord::Base
  belongs_to :post
  mount_uploader :output, OutputGifUploader

  validates :output, presence: true
end  

output_gif_uploader.rb

class OutputGifUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :fog

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

end

And this is the log and error:

Started POST "/posts" for ::1 at 2015-07-27 17:56:32 -0400 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"nXZJyEcn14qULwFsF3G59CajZeRG0QbsZDS/1MTXoCSzmOks24PL7eSeep2396wcDYrbhpgYX/1ISMkjOEEf0A==", "post"=>{"title"=>"afda"}, "post_attachments"=>{"image"=>[#, @original_filename="td2.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post_attachments[image][]\"; filename=\"td2.png\"\r\nContent-Type: image/png\r\n">, #, @original_filename="td3.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"post_attachments[image][]\"; filename=\"td3.jpg\"\r\nContent-Type: image/jpeg\r\n">]}, "commit"=>"gifMeDat"} (0.1ms) begin transaction SQL (1.1ms) INSERT INTO "posts" ("title", "created_at", "updated_at") VALUES (?, ?, ?) [["title", "afda"], ["created_at", "2015-07-27 21:56:32.917946"], ["updated_at", "2015-07-27 21:56:32.917946"]] (5.2ms) commit transaction (0.1ms) begin transaction SQL (1.4ms) INSERT INTO "post_attachments" ("image", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["image", "td2.png"], ["post_id", 97], ["created_at", "2015-07-27 21:56:33.031573"], ["updated_at", "2015-07-27 21:56:33.031573"]] (1.0ms) commit transaction (0.1ms) begin transaction SQL (0.3ms) INSERT INTO "post_attachments" ("image", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["image", "td3.jpg"], ["post_id", 97], ["created_at", "2015-07-27 21:56:33.983511"], ["updated_at", "2015-07-27 21:56:33.983511"]] (1.8ms) commit transaction Completed 500 in 2301ms

NameError (uninitialized constant Post::PostOutputs): app/controllers/posts_controller.rb:22:increate' `

Rendered /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (8.3ms) Rendered /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.2ms) Rendered /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.8ms) Rendered /Users/AYL/.rvm/rubies/ruby-2.1.5/lib/ruby/gems/2.1.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (33.1ms) Cannot render console with content type multipart/form-dataAllowed content types: [#, #

What is the right way to do this?

1
can you add the post output model?adamliesko
I just added post_output.rb and output_gif_uploader.rb filesYOHAN

1 Answers

0
votes

SOLUTION
in posts_controller.rb do
@post.create_post_output!(output: File.open(filename))
instead of
@post.post_output.create!(output: gif)