I have successfully implemented image uploading using carrierwave, fog and Amazon S3. In my imageuploader am using only fog as storage. But when i check my database i can see that just the file name is written instead of the amazon url. In my views its fetching correctly from aws without any issues.
Is it supposed to be like this? If so how the application figure out the exact url to s3?
imageuploader.rb`
# encoding: utf-8
class ImageUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
# include CarrierWave::MiniMagick
include CarrierWave::MiniMagick
#Include the sprockets-rails helper for Rails 4+ compatibility:
include Sprockets::Rails::Helper
# Choose what kind of storage to use for this uploader:
#storage :file
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :resize_to_fit => [50, 50]
# end
version :index_size do
process :resize_to_fill => [258, 173]
end
version :thumb_size do
process :resize_to_fill => [100, 100]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg" if original_filename
# end
end
models
class Picture < ActiveRecord::Base
belongs_to :listing
mount_uploader :image, ImageUploader
end
class Listing < ActiveRecord::Base
belongs_to :user
has_many :amenityconnecters
has_many :amenities, through: :amenityconnecters
has_many :pictures, :dependent => :destroy
accepts_nested_attributes_for :pictures, \
reject_if: proc{ |param| param[:image].blank? && param[:image_cache].blank? && param[:id].blank? }, \
allow_destroy: true
end
controllers
class ListingsController < ApplicationController
before_action :set_listing, only: [:show, :edit, :update, :destroy]
def new
@listing= Listing.new
end
def create
@listing=Listing.new(listing_params)
@listing.user_id=current_user.id
if @listing.save
render 'edit', notice: "Successfully created new listing"
else
render 'new'
end
end
def show
end
def index
@listings = Listing.all
end
def edit
end
def update
if @listing.update(listing_params)
flash[:success] = "Your listing was updated successfully"
redirect_to listing_path(@listing)
else
render 'edit'
end
end
private
def set_listing
@listing = Listing.find(params[:id])
end
def listing_params
params.require(:listing).permit(:title, :description, pictures_attributes: [:id, :image, :image_cache, :_destroy],:amenity_ids => [])
end
end
Aucun commentaire:
Enregistrer un commentaire