Back-End /Ruby On Rails

Ruby On Rails - Active Admin - Active Storage

rootkaien 2019. 3. 26. 13:55



Rails 5.2 부터 Active Storage가 지원된다. 

이걸 이용해서 이미지파일을 업로드해보장.. 



active storage사용법 참고

https://evilmartians.com/chronicles/rails-5-2-active-storage-and-beyond



active admin에는 요거 보고 하면된다.

https://medium.com/@maris.cilitis/using-ruby-on-rails-active-storage-image-uploads-for-active-admin-backed-resources-5638a9ca0b46




./bin/rails active_storage:install 


db/migrate에 파일이 생성된다. 


20190225100404_create_active_storage_tables.active_storage.rb


active_storage_attachments, active_storage_blobs 


./bin/rake db:migrate 




1
2
3
4
5
class Book < ApplicationRecord
  validates :title, presence: true
 
  has_one_attached :image
end
cs




1
2
3
4
5
ActiveAdmin.register Book do
  permit_params :title, :image
 
  form partial: 'form'
end
cs



1
2
3
4
5
6
7
8
<%= semantic_form_for [:admin, resource] do |f| %>
  <%= f.input :title %>
  <%= f.input :image, as: :file %>
  <%= f.actions do %>
    <%= f.action :submit, as: :button %>
    <%= f.action :cancel, as: :link %>
  <end %>
<end %>
cs


show 화면에 이미지가 출력되게 하기 위해서 show를 커스텀한다. 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
ActiveAdmin.register Book do
  permit_params :title, :image
 
  form partial: 'form'
 
  show do
    attributes_table do
      row :title
      row :image do |ad|
        image_tag url_for(ad.image)
      end
    end
  end
end
cs



[ undefined method `upload' for nil:NilClass ] 가 발생하였는데 이것이.. 


https://qiita.com/NaokiIshimura/items/c8aa4d55cb653ff0501a



1
2
3
4
# config/environments/development.rb
 
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local
cs