[英]Images are uploading to S3 but I cannot see them in my app
我對Rails還是很陌生,這是我第一次將文件上傳到S3。 在我的應用中,我使用了carrierwave gem,並且能夠選擇一個文件並保存。 載波和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
在此先感謝您的幫助!
嘗試改變
<%= @product.image_url %>
至
<%= image_tag(@product.image_url) %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.