Ich bin immer noch sehr neu bei Rails. Dies ist das erste Mal, dass ich Dateien auf S3 hochlade. In meiner App habe ich das Carrierwave-Juwel benutzt und kann eine Datei auswählen und speichern. Es scheint, dass die Verbindung zwischen carrierwave und S3 funktioniert, weil die Bilder in meinem S3-Bucket angezeigt werden. Das Problem ist, dass ich das Bild in meiner App nicht sehen kann und ich kann nicht herausfinden, warum das ist.Bilder werden auf S3 hochgeladen, aber ich kann sie nicht in meiner App sehen.
Hier ist mein 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:
# 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 (wo das Bild zeigt werden sollte):
<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
Vielen Dank im Voraus für jede Hilfe!
Es ist ein Tippfehler als auch in 'def enqueue_image',' key.presemt? '', Die key.present sein sollte? ' – archana