Sha256: f7767fdfc3a31bed5ec24fe860d67070930172e43209102fcc8585b62f0c1c7f
Contents?: true
Size: 1.72 KB
Versions: 8
Compression:
Stored size: 1.72 KB
Contents
require_dependency "pages_cms/application_controller" module PagesCms class Admin::ImagesController < ApplicationController before_action :logged_in_admin def index @images = Image.all @image = Image.new end def create validate_image = Image.new(image_params) if validate_image.validate_file_type(params[:image][:file]) user_named = params[:image][:name] uploaded_io = params[:image][:file] extension = uploaded_io.original_filename.split('.')[1] name = "#{Time.now.to_i}_#{user_named}.#{extension}" @image = Image.new(file: "/images/#{name}", name: user_named) if @image.save File.open(Rails.root.join('public', 'images', name), 'wb') do |file| file.write(uploaded_io.read) end if File.exists?(Rails.root.join('public', 'images', name)) flash[:success] = 'Successfully uploaded file.' redirect_to admin_images_path else @image.destroy flash[:danger] = 'Failed to create file' redirect_to admin_images_path end else flash[:danger] = "Error: #{@image.errors.full_messages.to_sentence}" redirect_to admin_images_path end else flash[:danger] = "Error #{validate_image.errors.full_messages.to_sentence}" redirect_to admin_images_path end end def destroy image = Image.find(params[:id]) image.destroy FileUtils.rm(Rails.root.join('public', 'images', image.file)) flash[:success] = 'Image deleted' redirect_to admin_images_path end private def image_params params.require(:image).permit(:file, :name) end end end
Version data entries
8 entries across 8 versions & 1 rubygems