0
我需要使用「aws.amazon.s3」服務保存圖片。 在寶石文件我有:Heroku CarrierWave S3 - 不知道如何保存圖片
gem 'carrierwave'
gem 'rmagick'
gem 'fog'
gem 'sprockets-rails'
我想用色器件登記控制器添加頭像到我的用戶。 我這樣做了:
1)rails g migration add_avatar_to_users avatar:string;
耙分貝:遷移
2)加入該行:
class User < ActiveRecord::Base
mount_uploader :avatar, AvatarUploader
end
3)創建應用程序/上傳/ avatar_uploader
# encoding: utf-8
class AvatarUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
# 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:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Create different versions of your uploaded files:
version :thumb do
process resize_to_fill: [140, 140]
process convert: 'jpeg'
# process :resize_to_fit => [140, 140]
end
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
end
4)創建的應用程序/配置/ INIT/carrierwawe.rb
CarrierWave.configure do |config|
config.storage = :fog
config.fog_directory = "app.#{Rails.env}" # required,
if Rails.env.production?
config.fog_credentials = {
:provider => 'AWS', # required
:aws_access_key_id => ENV["AWS_ACCESS_KEY"], # required
:aws_secret_access_key => ENV["AWS_SECRET_KEY"], # required
:region => 'eu-west-1'
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["AWS_BUCKET"] # required
config.fog_public = false
config.fog_attributes = {'Cache-Control'=>'max-age=60'}
config.fog_public = false # (optional) public readability, defaults to true
config.cache_dir = Rails.root.join('tmp','uploads')
config.root = Rails.root.join('tmp')
else
config.fog_credentials = {
:provider => 'AWS',
:aws_access_key_id => '',
:aws_secret_access_key => '',
:region => 'eu-west-1'
}
config.cache_dir = '/home/vagrant/uploads_tmp/tmp/uploads'
config.root = '/home/vagrant/uploads_tmp/tmp'
end
end
5)我的application_controller(加 - :username和:avatar,:avat ar_cache):
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password,
:password_confirmation, :remember_me , :avatar, :avatar_cache) }
devise_parameter_sanitizer.for(:account_update) {|u| u.permit(:username, :email, :password,
:password_confirmation, :current_password , :avatar, :avatar_cache) }
end
6)查看:視圖/設計/註冊/ new.html.haml
= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name),html: { role: "form" , :multipart => true }) do |f|
.form-group
= f.label :avatar do
= f.file_field :avatar , class: "form-control"
= f.hidden_field :avatar_cache
7)查看:視圖/設計/註冊/ edit.html.haml
.form-group
- if current_user.avatar.url.present?
= image_tag(current_user.avatar.url)
-#= f.label :remove_avatar do
-# = f.check_box :remove_avatar
= f.file_field :avatar, class: "form-control"
= f.hidden_field :avatar_cache
8)路線
devise_for :users
resources :dashboard
resources :users
root to: "users#show"
當我去到http://127.0.0.1:3000/users/sign_in
並填寫此推送提交後的所有字段。控制檯告訴我,...