繁体   English   中英

尝试使用Paperclip和Amazon API保存到S3

[英]Trying to save to S3 with Paperclip & Amazon API

嘿伙计们,我已经尝试查找我的具体问题的答案,但找不到解决方案。 我尝试了一切。 我有一个礼品模型,有回形针来保存图像。 礼品实例由亚马逊模型填充(该模型从亚马逊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链接,但它不起作用。 甚至尝试在图像上的礼品控制器中进行,但这也不起作用。 我假设回形针正在处理图像,即使它尚未创建,甚至不会让我之后创建礼物。 会很感激一些指导!

弄清楚了。 结束在表中添加image_url行,然后使用URI.parse(image_url)将其保存到图像,并将其保存到S3。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM