图片正在上传到S3,但我无法在我的应用中看到它们

时间:2016-03-25 21:03:21

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

我对rails很新,这是我第一次将文件上传到S3。在我的应用程序中,我使用了carrierwave gem,并且能够选择一个文件并保存。似乎carrierwave和S3之间的链接正在工作,因为图像显示在我的S3存储桶中。问题是我无法在我的应用程序中看到图像,我无法弄清楚为什么会这样。

这是我的控制器:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.all
    @uploader = Product.new.image
    @uploader.success_action_redirect = new_product_path
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new(key: params[:key])
  end

  # GET /products/1/edit
  def edit

  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render :show, status: :created, location: @product }
      else
        format.html { render :new }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to @product, notice: 'Product was successfully updated.' }
        format.json { render :show, status: :ok, location: @product }
      else
        format.html { render :edit }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:title, :description, :image, :price, :category, :subcategory)
    end
end

ImageUploder:

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWaveDirect::Uploader
  include CarrierWave::RMagick

 include CarrierWave::MimeTypes
  process :set_content_type

 version :thumb do
   process resize_to_fill: [200,200]
 end

end

show.html.erb(应该显示图片):

<p id="notice"><%= notice %></p>


<p>
  <strong>Title:</strong>
  <%= @product.title %>
</p>

<p>
  <strong>Description:</strong>
  <%= @product.description %>
</p>

<p>
  <strong>Image:</strong>
  <%= @product.image_url %>
</p>


<p>
  <strong>Price:</strong>
  <%= @product.price %>
</p>

<p>
  <strong>Category:</strong>
  <%= @product.category %>
</p>

<p>
  <strong>Subcategory:</strong>
  <%= @product.subcategory %>
</p>

<%= link_to 'Edit', edit_product_path(@product) %> |
<%= link_to 'Back', products_path %>

product.rb:

class Product < ActiveRecord::Base
    attr_accessor :image 
    mount_uploader :image, ImageUploader 

    after_save :enqueue_image

    def image_name
        File.basename(image.path || image.filename) if image
    end

    def enqueue_image
        ImageWorker.perform_async(id,key) if key.presemt?
    end

    class ImageWorker
        include Sidekiq::Worker

        def perform(id, key)
            product =  Product.find(id)
            product.key = key
            product.remote_image_url = product.image.direct_fog_url(with_path: true)
        end
    end

end

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试更改

<%= @product.image_url %>

<%= image_tag(@product.image_url) %>