起きている現象
CarrierWave、MiniMagick、ImageMagickを導入しましたが、導入後から画像投稿ができなくなってしまいました。正確にはアップローダーファイルに「process resize_to_fill: [300, 300, "Center"]」の記述をすると投稿ができなくなります。
この一文を消すと画像投稿自体はできるようになるので、リサイズする過程でなにかうまくいっていないのではないかと思います。
また、投稿してエラーは発生せず、もともと設定していた「投稿できませんでした」の文字が表示されます。
もし解決方法わかる方いらっしゃいましたらお力お借りしたいです。
image_uploader.rb
ruby
1class ImageUploader < CarrierWave::Uploader::Base 2 include CarrierWave::MiniMagick 3 # Include RMagick or MiniMagick support: 4 # include CarrierWave::RMagick 5 if Rails.env.production? 6 include Cloudinary::CarrierWave 7 else 8 storage :file 9 end 10 11 # Choose what kind of storage to use for this uploader: 12 storage :file 13 # storage :fog 14 15 # Override the directory where uploaded files will be stored. 16 # This is a sensible default for uploaders that are meant to be mounted: 17 def store_dir 18 "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 19 end 20 # def default_url(*args) 21 # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 22 # end 23 #この部分がアップロードした写真を設定できるようにする記述とデフォルトの写真を設定している記述 24 25 # Provide a default URL as a default if there hasn't been a file uploaded: 26 # def default_url(*args) 27 # # For Rails 3.1+ asset pipeline compatibility: 28 # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) 29 # 30 # "/images/fallback/" + [version_name, "default.png"].compact.join('_') 31 # end 32 33 # Process files as they are uploaded: 34 # process scale: [200, 300] 35 # 36 # def scale(width, height) 37 # # do something 38 # end 39 40 # Create different versions of your uploaded files: 41 # version :thumb do 42 # process resize_to_fit: [50, 50] 43 # end 44 45 # Add a white list of extensions which are allowed to be uploaded. 46 # For images you might use something like this: 47 def extension_whitelist 48 %w(jpg jpeg gif png) 49 end 50 51 # Override the filename of the uploaded files: 52 # Avoid using model.id or version_name here, see uploader/store.rb for details. 53 # def filename 54 # "something.jpg" if original_filename 55 # end 56 process resize_to_fill: [300, 300, "Center"] 57 58end 59
micro posts_controller
ruby
1class MicropostsController < ApplicationController 2 before_action :require_user_logged_in 3 before_action :correct_user, only: [:destroy, :edit] 4 5 def index 6 @microposts = Micropost.all.order(created_at: :desc) 7 end 8 9 def show 10 @micropost = Micropost.find(params[:id]) 11 end 12 13 def new 14 @micropost = Micropost.new 15 end 16 17 def create 18 @micropost = current_user.microposts.build(micropost_params) 19 if @micropost.save 20 flash[:success] = '投稿完了' 21 redirect_to microposts_url 22 else 23 @microposts = Micropost.all.order(created_at: :desc) 24 flash.now[:danger] = '投稿できませんでした。' 25 render 'microposts/index' 26 end 27 end 28 29 def destroy 30 @micropost.destroy 31 flash[:success] = 'メッセージ削除' 32 redirect_back(fallback_location: root_path) 33 end 34 35 def update 36 end 37 38 def edit 39 end 40 41 42 private 43 44 def micropost_params 45 params.require(:micropost).permit(:title, :day, :content, :place, :number, :image) 46 end 47 48 def correct_user 49 @micropost = current_user.microposts.find_by(id: params[:id]) 50 unless @micropost 51 redirect_to root_url 52 end 53 end 54end
micropost.rb
ruby
1class Micropost < ApplicationRecord 2 belongs_to :user 3 validates :content, presence: true, length: { maximum: 1000 } 4 has_many :likes, dependent: :destroy 5 has_many :liked, through: :likes, source: :user 6 has_many :joins 7 has_many :joined, through: :joins, source: :user 8 9 THUMBNAIL_SIZE = [300, 300] 10 mount_uploader :image, ImageUploader 11end 12
Gemfile
ruby
1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '2.7.1' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 7gem 'rails', '~> 5.2.2' 8# Use mysql as the database for Active Record 9gem 'mysql2', '>= 0.4.4', '< 0.6.0' 10# Use Puma as the app server 11gem 'puma', '~> 3.11' 12# Use SCSS for stylesheets 13gem 'sass-rails', '~> 5.0' 14# Use Uglifier as compressor for JavaScript assets 15gem 'uglifier', '>= 1.3.0' 16# See https://github.com/rails/execjs#readme for more supported runtimes 17# gem 'mini_racer', platforms: :ruby 18 19# Use CoffeeScript for .coffee assets and views 20gem 'coffee-rails', '~> 4.2' 21# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 22gem 'turbolinks', '~> 5' 23# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 24gem 'jbuilder', '~> 2.5' 25# Use Redis adapter to run Action Cable in production 26# gem 'redis', '~> 4.0' 27# Use ActiveModel has_secure_password 28gem 'bcrypt', '~> 3.1.7' 29 30# Use Capistrano for deployment 31# gem 'capistrano-rails', group: :development 32 33# Reduces boot times through caching; required in config/boot.rb 34gem 'bootsnap', '>= 1.1.0', require: false 35 36group :development, :test do 37 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 38 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 39end 40 41group :development do 42 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 43 gem 'web-console', '>= 3.3.0' 44 gem 'listen', '>= 3.0.5', '< 3.2' 45 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 46 gem 'spring' 47 gem 'spring-watcher-listen', '~> 2.0.0' 48end 49 50 51# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 52gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 53 54#ページネーション 55gem 'kaminari' 56 57# Fontawsome 58# gem 'font-awesome-sass', '~> 5.4.1' 59 60# 画像用 61gem 'carrierwave', '~> 1.3', '>= 1.3.1' 62gem 'mini_magick' 63gem 'cloudinary' 64gem 'rmagick' 65 66# JQUERY導入 67gem 'jquery-rails' 68
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/14 10:41