Class: Spina::Admin::ImagesController
- Inherits:
-
AdminController
- Object
- ActionController::Base
- Spina::ApplicationController
- AdminController
- Spina::Admin::ImagesController
- Defined in:
- app/controllers/spina/admin/images_controller.rb
Instance Method Summary collapse
- #add_to_media_folder ⇒ Object
-
#create ⇒ Object
There’s no file validation yet in ActiveStorage We do two things to reduce errors right now: 1.
- #destroy ⇒ Object
- #index ⇒ Object
Methods inherited from AdminController
Instance Method Details
#add_to_media_folder ⇒ Object
37 38 39 40 41 |
# File 'app/controllers/spina/admin/images_controller.rb', line 37 def add_to_media_folder @media_folder = MediaFolder.find(params[:id]) @media_folder.images << Image.find(params[:image_id]) render json: @media_folder end |
#create ⇒ Object
There’s no file validation yet in ActiveStorage We do two things to reduce errors right now:
-
We add accept=“image/*” to the image form
-
We destroy the entire record if the uploaded file is not an image
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/controllers/spina/admin/images_controller.rb', line 18 def create @images = params[:image][:files].map do |file| # Create the image and attach the file image = Image.create(media_folder_id: params[:media_library]) image.file.attach(file) # Was it not an image after all? DESTROY IT image.destroy and next unless image.file.image? image end.compact end |
#destroy ⇒ Object
31 32 33 34 35 |
# File 'app/controllers/spina/admin/images_controller.rb', line 31 def destroy @image = Image.find(params[:id]) @image.destroy redirect_back fallback_location: spina.admin_images_url end |
#index ⇒ Object
8 9 10 11 12 |
# File 'app/controllers/spina/admin/images_controller.rb', line 8 def index I18n.t('spina.website.images'), admin_images_path @media_folders = MediaFolder.order(:name) @images = Image.sorted.where(media_folder_id: nil).with_attached_file.page(params[:page]) end |