【问题标题】:Trying to save to S3 with Paperclip & Amazon API尝试使用 Paperclip 和 Amazon API 保存到 S3
【发布时间】:2017-01-28 04:21:00
【问题描述】:

大家好,我已尝试查找特定问题的答案,但找不到解决方案。我什么都试过了。 我有一个带有回形针的礼物模型来保存图像。礼物实例由亚马逊模型填充(它从亚马逊 api 获取产品详细信息,包括图像、名称和价格),但我无法将图像保存到 S3。 我认为我的问题与回形针将其转换为礼品控制器中“新”路由上的亚马逊 S3 链接这一事实有关,而不是在它实际创建礼品时。 这是一些代码:

class GiftsController < ApplicationController
  skip_before_action :verify_authenticity_token
  before_action :amazon_url, only: [:new, :new_extension]
  before_action :authenticate_user!

  def new
    @friend = Friend.find_by_id(params[:id])
    if @gift_data.failure == nil
      @gift = Gift.new(
          name: @gift_data.name,
          image: @gift_data.image,
          price: @gift_data.price
      )
    else
      @gift = Gift.new()
      if params[:search]
        @failure = @gift_data.failure
        respond_to do |format|
          format.html
          format.json { render :json => {gift: @gift} }
        end
      end
    end
  end

还有我的礼物模型:

class Gift < ActiveRecord::Base
  belongs_to :category
  has_many :ideas
  has_many :friends, :through => :ideas
  # this friends association doesn't work, need to troubleshoot more or figure that out
  # also figure out whether we need it
  has_many :goods


  has_attached_file :image,
                    :storage  => :s3,
                    :styles => { :medium => "370x370", :thumb => "100x100" }
                    validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

  validates_presence_of :name
  # validates_presence_of :category_id
  # validates :url, :url => {:allow_blank => false}
  # validates :price, presence: true, format: {with: /\A\d+(?:\.\d{0,2})?\z/}, numericality: {greater_than: 0, less_than: 1000000}

end

我尝试在礼物模型中添加一个方法来 URI.parse 链接,但它不起作用。甚至尝试在图像上的礼品控制器中执行此操作,但这也不起作用。我假设回形针正在处理图像,即使它尚未创建,甚至不会让我在之后创建礼物。希望得到一些指导!

【问题讨论】:

  • 嗯,我可能缺少一些东西,但你在哪里保存@gift?你创造它。但保存在哪里?您应该添加 .save (Gift.new(params).save)
  • 新路线尚未保存。我有一个创建方法可以做到这一点

标签: ruby-on-rails amazon-s3 paperclip


【解决方案1】:

想通了。最终在表中添加了一个 image_url 行,然后使用 URI.parse(image_url) 将其保存到图像并将其保存到 S3。

【讨论】:

    猜你喜欢
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2011-12-13
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多