teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

config/initializers/carrierwave.rbも追記しました。

2020/11/03 08:15

投稿

amby
amby

スコア40

title CHANGED
File without changes
body CHANGED
@@ -97,6 +97,27 @@
97
97
  <% end %>
98
98
  </div>
99
99
  ```
100
+ ### 追記 config/initializers/carrierwave.rb
101
+ ```ここに言語を入力
102
+ require 'carrierwave/storage/abstract'
103
+ require 'carrierwave/storage/file'
104
+ require 'carrierwave/storage/fog'
100
105
 
106
+ CarrierWave.configure do |config|
107
+ config.storage = :fog
108
+ config.fog_provider = 'fog/aws'
109
+ config.fog_credentials = {
110
+ provider: 'AWS',
111
+ aws_access_key_id: 'ここにはアクセスキーを記入しています',
112
+ aws_secret_access_key: 'ここにはシークレットキーを記入しています',
113
+ region: 'ap-northeast-1'
101
114
 
115
+ }
116
+
117
+ config.fog_directory = 'バケット名を記入しています'
118
+ config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/バケット名を記入しています'
119
+ end
120
+ ```
121
+
122
+
102
123
  その他、追加すべきものがあればご指示ください。

1

画像表示に関するコードを追記しました。

2020/11/03 08:15

投稿

amby
amby

スコア40

title CHANGED
File without changes
body CHANGED
@@ -10,5 +10,93 @@
10
10
  ・「config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/バケット名'」を削除してみました。
11
11
  ・S3の「アクセス権限」から「ブロックパブリックアクセス 」のブロックを全てオフにしてみました。
12
12
 
13
+ ### 追記 microposts.rb
14
+ ```ここに言語を入力
15
+ class Micropost < ApplicationRecord
16
+ belongs_to :user
17
+ validates :content, presence: true, length: { maximum: 1000 }
18
+ has_many :likes, dependent: :destroy
19
+ has_many :liked, through: :likes, source: :user
20
+ has_many :joins, dependent: :destroy
21
+ has_many :joined, through: :joins, source: :user
22
+ has_many :comments, dependent: :destroy
13
23
 
24
+ THUMBNAIL_SIZE = [300, 300]
25
+ mount_uploader :image, ImageUploader
26
+
27
+ geocoded_by :place
28
+ after_validation :geocode
29
+ end
30
+ ```
31
+ ### 追記 image_uploader.rb
32
+ ```ここに言語を入力
33
+ class ImageUploader < CarrierWave::Uploader::Base
34
+ include CarrierWave::MiniMagick
35
+ require 'mini_magick'
36
+ # Include RMagick or MiniMagick support:
37
+ # include CarrierWave::RMagick
38
+ if Rails.env.production?
39
+ include Cloudinary::CarrierWave
40
+ else
41
+ storage :fog
42
+ end
43
+
44
+ # Choose what kind of storage to use for this uploader:
45
+ # storage :file
46
+ storage :fog
47
+
48
+ # Override the directory where uploaded files will be stored.
49
+ # This is a sensible default for uploaders that are meant to be mounted:
50
+ def store_dir
51
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
52
+ end
53
+ # def default_url(*args)
54
+ # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
55
+
56
+ # Provide a default URL as a default if there hasn't been a file uploaded:
57
+ # def default_url(*args)
58
+ # # For Rails 3.1+ asset pipeline compatibility:
59
+ # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
60
+ #
61
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
62
+ # end
63
+
64
+ # Process files as they are uploaded:
65
+ # process scale: [200, 300]
66
+ #
67
+ # def scale(width, height)
68
+ # # do something
69
+ # end
70
+
71
+ # Create different versions of your uploaded files:
72
+ # version :thumb do
73
+ # process resize_to_fit: [50, 50]
74
+ # end
75
+
76
+ # Add a white list of extensions which are allowed to be uploaded.
77
+ # For images you might use something like this:
78
+ def extension_whitelist
79
+ %w(jpg jpeg gif png)
80
+ end
81
+
82
+ # Override the filename of the uploaded files:
83
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
84
+ # def filename
85
+ # "something.jpg" if original_filename
86
+ # end
87
+ process resize_to_fill: [300, 300, "Center"]
88
+
89
+ end
90
+ ```
91
+ ### 追記 _micropost.html.erbで画像表示している部分
92
+ ```ここに言語を入力
93
+ <% if micropost.image? %>
94
+ <%= image_tag micropost.image.url, class:"micropost_image" %>
95
+ <% else %>
96
+ <%= image_tag 'no_image.png', class:"micropost_image" %>
97
+ <% end %>
98
+ </div>
99
+ ```
100
+
101
+
14
102
  その他、追加すべきものがあればご指示ください。