【问题标题】:Rails RMagick output carrierwaveRails RMagick 输出载波
【发布时间】:2015-10-18 07:13:09
【问题描述】:

我正在使用 RMagick gem 制作一个将图像转换为动画 gif 的 rails 应用程序。有Post 模型,即has_many post_attachmentshas_one post_output。这个想法是用户可以上传多张图像并被重定向到带有生成动画 gif 的页面。我在通过载波、AWS S3 和雾将 gif 附加到 post_output(它有一个名为“输出”的属性)时遇到问题。

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

这是日志和错误:

在 2015-07-27 17:56:32 -0400 开始为 ::1 发布“/posts” PostsController#create 作为 HTML 处理 参数:{"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="内容处置:表单数据;名称=\"post_attachments[图像][]\";文件名=\"td3.jpg\"\r\n内容类型:图像/jpeg\r\n">]},"提交"=>"gifMeDat"} (0.1ms) 开始交易 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) 提交事务 (0.1ms) 开始交易 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) 提交事务 (0.1ms) 开始交易 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) 提交事务 在 2301 毫秒内完成 500 个

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

渲染 /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) 渲染/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) 渲染/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) 渲染/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在救援/布局内 (33.1ms) 无法呈现内容类型为 multipart/form-dataAllowed 内容类型的控制台:[#, #

这样做的正确方法是什么?

【问题讨论】:

  • 可以添加post输出模型吗?
  • 我刚刚添加了 post_output.rb 和 output_gif_uploader.rb 文件

标签: ruby-on-rails ruby imagemagick carrierwave rmagick


【解决方案1】:

解决方案
在 posts_controller.rb 中执行
@post.create_post_output!(output: File.open(filename))
而不是
@post.post_output.create!(output: gif)

【讨论】:

    猜你喜欢
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 2015-01-26
    • 1970-01-01
    相关资源
    最近更新 更多