質問編集履歴
2
誤字を修正しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
ローカルサーバで動かしていた時には問題なく表示されていました。
|
8
8
|
|
9
|
-
現在は画像を投稿するとpublicフォルダに{post
|
9
|
+
現在は画像を投稿するとpublicフォルダに{post_id}.jpgとして保存され、表示する際はpost_idを参照して表示しています。
|
10
10
|
|
11
11
|
そこで、保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにしたいです。
|
12
12
|
|
1
現在のコードを追加しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,3 +15,359 @@
|
|
15
15
|
画像の保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにするにはどのようにすれば良いでしょうか?
|
16
16
|
|
17
17
|
初心者なので見当違いの質問でしたらすみません。
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
|
23
|
+
class PostsController < ApplicationController
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
before_action :authenticate_user
|
28
|
+
|
29
|
+
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def index
|
34
|
+
|
35
|
+
@posts = Post.all.order(created_at: :desc)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
def show
|
42
|
+
|
43
|
+
@post = Post.find_by(id: params[:id])
|
44
|
+
|
45
|
+
@user = @post.user
|
46
|
+
|
47
|
+
@likes_count = Like.where(post_id: @post.id).count
|
48
|
+
|
49
|
+
@comment = Comment.find_by(id: params[:id])
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def new
|
56
|
+
|
57
|
+
@post = Post.new
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
def create
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
@post = Post.new(content:params[:content],
|
68
|
+
|
69
|
+
user_id: @current_user.id,
|
70
|
+
|
71
|
+
)
|
72
|
+
|
73
|
+
@post.save
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
@user = User.find(@current_user.id)
|
78
|
+
|
79
|
+
@user.user_exp += 5
|
80
|
+
|
81
|
+
@user.save
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
if params[:image]
|
86
|
+
|
87
|
+
@post.image_name = "#{@post.id}.jpg"
|
88
|
+
|
89
|
+
image = params[:image]
|
90
|
+
|
91
|
+
File.binwrite("public/post_images/#{@post.image_name}",image.read)
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
if @post.save
|
98
|
+
|
99
|
+
redirect_to("/posts/index")
|
100
|
+
|
101
|
+
else
|
102
|
+
|
103
|
+
render("posts/new")
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def edit
|
112
|
+
|
113
|
+
@post = Post.find_by(id: params[:id])
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
def update
|
120
|
+
|
121
|
+
@post = Post.find_by(id: params[:id])
|
122
|
+
|
123
|
+
@post.content = params[:content]
|
124
|
+
|
125
|
+
if @post.save
|
126
|
+
|
127
|
+
redirect_to("/posts/index")
|
128
|
+
|
129
|
+
else
|
130
|
+
|
131
|
+
render("posts/edit")
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
def destroy
|
140
|
+
|
141
|
+
@post = Post.find_by(id: params[:id])
|
142
|
+
|
143
|
+
@post.destroy
|
144
|
+
|
145
|
+
redirect_to("/posts/index")
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
@user = User.find(@current_user.id)
|
150
|
+
|
151
|
+
@user.user_exp -= 5
|
152
|
+
|
153
|
+
@user.save
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def ensure_correct_user
|
160
|
+
|
161
|
+
@post = Post.find_by(id: params[:id])
|
162
|
+
|
163
|
+
if @post.user_id != @current_user.id
|
164
|
+
|
165
|
+
flash[:notice] = "権限がありません"
|
166
|
+
|
167
|
+
redirect_to("/posts/index")
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
```
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
|
187
|
+
<div class="main posts-new">
|
188
|
+
|
189
|
+
<div class="container">
|
190
|
+
|
191
|
+
<h1 class="form-heading">New Post</h1>
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
<%= form_tag("/posts/create",{multipart:true}) do %>
|
196
|
+
|
197
|
+
<div class="form">
|
198
|
+
|
199
|
+
<div class="form-body">
|
200
|
+
|
201
|
+
<% @post.errors.full_messages.each do |message| %>
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
<div class="form-error">
|
206
|
+
|
207
|
+
<%= message %>
|
208
|
+
|
209
|
+
</div>
|
210
|
+
|
211
|
+
<% end %>
|
212
|
+
|
213
|
+
<textarea name="content"><%= @post.content %></textarea>
|
214
|
+
|
215
|
+
<p>画像</p>
|
216
|
+
|
217
|
+
<input name="image" type="file">
|
218
|
+
|
219
|
+
<input type="submit" value="Post">
|
220
|
+
|
221
|
+
</div>
|
222
|
+
|
223
|
+
</div>
|
224
|
+
|
225
|
+
<% end %>
|
226
|
+
|
227
|
+
</div>
|
228
|
+
|
229
|
+
</div>
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
|
239
|
+
<div class="main posts-show">
|
240
|
+
|
241
|
+
<div class="container">
|
242
|
+
|
243
|
+
<div class="posts-show-item">
|
244
|
+
|
245
|
+
<div class="post-user-name">
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
<img src="<%= "/user_images/#{@user.image_name}" %>">
|
250
|
+
|
251
|
+
<%= link_to(@user.name, "/users/#{@user.id}") %>
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
</div>
|
256
|
+
|
257
|
+
<p>
|
258
|
+
|
259
|
+
<%= @post.content %>
|
260
|
+
|
261
|
+
</p>
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
<%= @post.created_at %>
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
<% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %>
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
<%= link_to( "/likes/#{@post.id}/destroy", {method: "post"}) do %>
|
280
|
+
|
281
|
+
<span class="fa fa-heart like-btn-unlike"></span>
|
282
|
+
|
283
|
+
<% end %>
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
<% else %>
|
288
|
+
|
289
|
+
<%= link_to( "/likes/#{@post.id}/create", {method: "post"}) do %>
|
290
|
+
|
291
|
+
<span class="fa fa-heart like-btn"></span>
|
292
|
+
|
293
|
+
<% end %>
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
<% end %>
|
298
|
+
|
299
|
+
<%= @likes_count %>
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
<% if @post.user_id == @current_user.id %>
|
304
|
+
|
305
|
+
<div class="post-menus">
|
306
|
+
|
307
|
+
<%= link_to("編集", "/posts/#{@post.id}/edit") %>
|
308
|
+
|
309
|
+
<%= link_to("削除", "/posts/#{@post.id}/destroy",{method:"post"}) %>
|
310
|
+
|
311
|
+
</div>
|
312
|
+
|
313
|
+
<% end %>
|
314
|
+
|
315
|
+
</div>
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
<% if @post.image_name != nil %>
|
320
|
+
|
321
|
+
<p class="post_image">
|
322
|
+
|
323
|
+
<img width="570px" src="<%= "/post_images/#{@post.image_name}" %>">
|
324
|
+
|
325
|
+
</p>
|
326
|
+
|
327
|
+
<% end %>
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
<div class ="post_comment">
|
332
|
+
|
333
|
+
<%= form_tag("/comments/#{@post.id}/create",{multipart:true}) do %>
|
334
|
+
|
335
|
+
<textarea name="content" cols=50 rows=10 ></textarea>
|
336
|
+
|
337
|
+
<input type="submit" value="Encourage!!!">
|
338
|
+
|
339
|
+
</div>
|
340
|
+
|
341
|
+
<% end %>
|
342
|
+
|
343
|
+
```
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
```ruby
|
348
|
+
|
349
|
+
class Post < ApplicationRecord
|
350
|
+
|
351
|
+
validates :content, {presence: true,length:{maximum:140}}
|
352
|
+
|
353
|
+
validates :user_id, {presence: true}
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
def user
|
358
|
+
|
359
|
+
return User.find_by(id: self.user_id)
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
def comments
|
366
|
+
|
367
|
+
return Comment.where(post_id: self.id)
|
368
|
+
|
369
|
+
end
|
370
|
+
|
371
|
+
end
|
372
|
+
|
373
|
+
```
|