質問編集履歴

2

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

2020/11/03 08:15

投稿

amby
amby

スコア40

test CHANGED
File without changes
test CHANGED
@@ -196,6 +196,48 @@
196
196
 
197
197
  ```
198
198
 
199
+ ### 追記 config/initializers/carrierwave.rb
200
+
201
+ ```ここに言語を入力
202
+
203
+ require 'carrierwave/storage/abstract'
204
+
205
+ require 'carrierwave/storage/file'
206
+
207
+ require 'carrierwave/storage/fog'
208
+
209
+
210
+
211
+ CarrierWave.configure do |config|
212
+
213
+ config.storage = :fog
214
+
215
+ config.fog_provider = 'fog/aws'
216
+
217
+ config.fog_credentials = {
218
+
219
+ provider: 'AWS',
220
+
221
+ aws_access_key_id: 'ここにはアクセスキーを記入しています',
222
+
223
+ aws_secret_access_key: 'ここにはシークレットキーを記入しています',
224
+
225
+ region: 'ap-northeast-1'
226
+
227
+
228
+
229
+ }
230
+
231
+
232
+
233
+ config.fog_directory = 'バケット名を記入しています'
234
+
235
+ config.asset_host = 'https://s3-ap-northeast-1.amazonaws.com/バケット名を記入しています'
236
+
237
+ end
238
+
239
+ ```
240
+
199
241
 
200
242
 
201
243
 

1

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

2020/11/03 08:15

投稿

amby
amby

スコア40

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,182 @@
22
22
 
23
23
 
24
24
 
25
+ ### 追記 microposts.rb
26
+
27
+ ```ここに言語を入力
28
+
29
+ class Micropost < ApplicationRecord
30
+
31
+ belongs_to :user
32
+
33
+ validates :content, presence: true, length: { maximum: 1000 }
34
+
35
+ has_many :likes, dependent: :destroy
36
+
37
+ has_many :liked, through: :likes, source: :user
38
+
39
+ has_many :joins, dependent: :destroy
40
+
41
+ has_many :joined, through: :joins, source: :user
42
+
43
+ has_many :comments, dependent: :destroy
44
+
45
+
46
+
47
+ THUMBNAIL_SIZE = [300, 300]
48
+
49
+ mount_uploader :image, ImageUploader
50
+
51
+
52
+
53
+ geocoded_by :place
54
+
55
+ after_validation :geocode
56
+
57
+ end
58
+
59
+ ```
60
+
61
+ ### 追記 image_uploader.rb
62
+
63
+ ```ここに言語を入力
64
+
65
+ class ImageUploader < CarrierWave::Uploader::Base
66
+
67
+ include CarrierWave::MiniMagick
68
+
69
+ require 'mini_magick'
70
+
71
+ # Include RMagick or MiniMagick support:
72
+
73
+ # include CarrierWave::RMagick
74
+
75
+ if Rails.env.production?
76
+
77
+ include Cloudinary::CarrierWave
78
+
79
+ else
80
+
81
+ storage :fog
82
+
83
+ end
84
+
85
+
86
+
87
+ # Choose what kind of storage to use for this uploader:
88
+
89
+ # storage :file
90
+
91
+ storage :fog
92
+
93
+
94
+
95
+ # Override the directory where uploaded files will be stored.
96
+
97
+ # This is a sensible default for uploaders that are meant to be mounted:
98
+
99
+ def store_dir
100
+
101
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
102
+
103
+ end
104
+
105
+ # def default_url(*args)
106
+
107
+ # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
108
+
109
+
110
+
111
+ # Provide a default URL as a default if there hasn't been a file uploaded:
112
+
113
+ # def default_url(*args)
114
+
115
+ # # For Rails 3.1+ asset pipeline compatibility:
116
+
117
+ # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
118
+
119
+ #
120
+
121
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
122
+
123
+ # end
124
+
125
+
126
+
127
+ # Process files as they are uploaded:
128
+
129
+ # process scale: [200, 300]
130
+
131
+ #
132
+
133
+ # def scale(width, height)
134
+
135
+ # # do something
136
+
137
+ # end
138
+
139
+
140
+
141
+ # Create different versions of your uploaded files:
142
+
143
+ # version :thumb do
144
+
145
+ # process resize_to_fit: [50, 50]
146
+
147
+ # end
148
+
149
+
150
+
151
+ # Add a white list of extensions which are allowed to be uploaded.
152
+
153
+ # For images you might use something like this:
154
+
155
+ def extension_whitelist
156
+
157
+ %w(jpg jpeg gif png)
158
+
159
+ end
160
+
161
+
162
+
163
+ # Override the filename of the uploaded files:
164
+
165
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
166
+
167
+ # def filename
168
+
169
+ # "something.jpg" if original_filename
170
+
171
+ # end
172
+
173
+ process resize_to_fill: [300, 300, "Center"]
174
+
175
+
176
+
177
+ end
178
+
179
+ ```
180
+
181
+ ### 追記 _micropost.html.erbで画像表示している部分
182
+
183
+ ```ここに言語を入力
184
+
185
+ <% if micropost.image? %>
186
+
187
+ <%= image_tag micropost.image.url, class:"micropost_image" %>
188
+
189
+ <% else %>
190
+
191
+ <%= image_tag 'no_image.png', class:"micropost_image" %>
192
+
193
+ <% end %>
194
+
195
+ </div>
196
+
197
+ ```
198
+
199
+
200
+
25
201
 
26
202
 
27
203
  その他、追加すべきものがあればご指示ください。