内容
AWSでデプロイを試みています。S3を使用して画像投稿ができるサイトを作っていますが、投稿された画像が下記のように表示されません。グーグルの開発者ツールのコンソールをみると「Failed to load resource: the server responded with a status of 404 ()」という表記になっています。
S3自体に画像はアップロードされており、読み込めていないだけだと思うのですが、どこを修正すべきかわからず、ご教示いただきたいです。
resource: the server responded with a status of 404 ()の画像URLをクリックした時
microposts.rb
class Micropost < ApplicationRecord belongs_to :user validates :content, presence: true, length: { maximum: 1000 } has_many :likes, dependent: :destroy has_many :liked, through: :likes, source: :user has_many :joins, dependent: :destroy has_many :joined, through: :joins, source: :user has_many :comments, dependent: :destroy THUMBNAIL_SIZE = [300, 300] mount_uploader :image, ImageUploader geocoded_by :place after_validation :geocode end
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base include CarrierWave::MiniMagick require 'mini_magick' # Include RMagick or MiniMagick support: # include CarrierWave::RMagick if Rails.env.production? include Cloudinary::CarrierWave else storage :fog end # Choose what kind of storage to use for this uploader: # storage :file storage :fog # Override the directory where uploaded files will be stored. # This is a sensible default for uploaders that are meant to be mounted: def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end # def default_url(*args) # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url(*args) # # For Rails 3.1+ asset pipeline compatibility: # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: # process scale: [200, 300] # # def scale(width, height) # # do something # end # Create different versions of your uploaded files: # version :thumb do # process resize_to_fit: [50, 50] # end # Add a white list of extensions which are allowed to be uploaded. # For images you might use something like this: def extension_whitelist %w(jpg jpeg gif png) end # Override the filename of the uploaded files: # Avoid using model.id or version_name here, see uploader/store.rb for details. # def filename # "something.jpg" if original_filename # end process resize_to_fill: [300, 300, "Center"] end
_micropost.html.erbで画像表示している部分
<% if micropost.image? %> <%= image_tag micropost.image.to_s, class:"micropost_image" %> <% else %> <%= image_tag 'no_image.png', class:"micropost_image" %> <% end %>
config/initializers/carrierwave.rb(keyは若干変えています)
require 'carrierwave/storage/abstract' require 'carrierwave/storage/file' require 'carrierwave/storage/fog' CarrierWave.configure do |config| config.storage = :fog config.fog_provider = 'fog/aws' config.fog_credentials = { provider: 'AWS', aws_access_key_id: 'AKIAQFS7xxxZ6FWVOKW6', aws_secret_access_key: "/+0I953xxxxVNJtM/3s3gYlWUPd+rB8xxxkvN8ig", region: 'ap-northeast-1' } config.fog_directory = 'bucketrugby' config.asset_host = 'http://bucketrugby.s3-ap-northeast-1.amazonaws.com' end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/11/09 22:51
2020/11/10 12:49
2020/11/10 13:33