Open neutral2010 opened 2 years ago
rails active_storage:install
rails db:migrate
ruby~/fBootcamp/fjord-books_app my-user_icon*
❯ bundle install
.
.
.
Bundle complete! 27 Gemfile dependencies, 106 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
~/fBootcamp/fjord-books_app my-user_icon*
❯ rails active_storage:install
Copied migration 20220407124851_create_active_storage_tables.active_storage.rb from active_storage
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
gem 'mini_magick', '~> 4.11'
mini_magick | RubyGems.org | コミュニティのGemホスティングサービス
✔︎ `config/storage.yml :ファイルの格納先を宣言
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
✔︎ config/environments/development.rb
:利用するサービスをActive Storageに認識させる
ActiveStorageにはバリデーションの機能がない
validates :icon, presence: true, blob: { content_type: ['image/png', 'image/jpg', 'image/jpeg'] }
lidates :icon, content_type: [:jpg, :png, :gif]
<%= f.file_field :avatar, accept: "image/jpg,image/png,image/gif" %>
1.や2.のgemを使った場合、エラーメッセージは自分で付ける?
エラーだった。
# app/views/devise/registrations/_profile_fields.html.erb
<div class="field">
<%= f.label :icon %>
<% if @user.icon.attached? && @user.icon.variable? %>
<%= image_tag @user.icon.variant(resize_to_fill: [50, 50]) %>
<% end %>
<%= f.file_field :icon %>
</div>
with_attached_name
を使う
# app/controllers/users_controller.rb
def index
@users = User.with_attached_avatar.order(:id).page(params[:page])
end
ログの量が全然違う!
ActiveStorageで画像アップロードを実装する