简体   繁体   English

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

[英]Images are uploading to S3 but I cannot see them in my app

I am still very new to rails is this is the first time I am uploading files to S3. 我对Rails还是很陌生,这是我第一次将文件上传到S3。 In my app I have used the carrierwave gem and am able to choose a file and save. 在我的应用中,我使用了carrierwave gem,并且能够选择一个文件并保存。 It seems like the link between carrierwave and S3 are working because the images are showing up in my S3 bucket. 载波和S3之间的链接似乎正常工作,因为图像显示在我的S3存储桶中。 The problem is I can not see the image in my app and I cannot figure out why this is. 问题是我看不到我的应用程序中的图像,也无法弄清为什么。

Here is my controller: 这是我的控制器:

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: 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 (where the image should be showing up): 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: 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

Thanks in advance for any help! 在此先感谢您的帮助!

Try changing 尝试改变

<%= @product.image_url %>

to

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

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

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