質問編集履歴
2
誤字を修正しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
現在、progateなどで勉強しつつ、railsを用いて簡単なsnsを作っています。
|
3
3
|
herokuを使って公開するところまではできたのですが、投稿した画像が一定時間たつと消えてしまいます。
|
4
4
|
ローカルサーバで動かしていた時には問題なく表示されていました。
|
5
|
-
現在は画像を投稿するとpublicフォルダに{
|
5
|
+
現在は画像を投稿するとpublicフォルダに{post_id}.jpgとして保存され、表示する際はpost_idを参照して表示しています。
|
6
6
|
そこで、保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにしたいです。
|
7
7
|
色々と調べて見たのですが、carrierwaveなどのgemを使わずに画像投稿機能を実装している例がなく、また今から画像投稿機能部分を全てcarrierwaveなどのgemを使ったものに変更するのも、知識がないために少し怖いです。
|
8
8
|
画像の保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにするにはどのようにすれば良いでしょうか?
|
1
現在のコードを追加しました
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,4 +6,182 @@
|
|
6
6
|
そこで、保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにしたいです。
|
7
7
|
色々と調べて見たのですが、carrierwaveなどのgemを使わずに画像投稿機能を実装している例がなく、また今から画像投稿機能部分を全てcarrierwaveなどのgemを使ったものに変更するのも、知識がないために少し怖いです。
|
8
8
|
画像の保存先をpublicフォルダではなく、AWSのS3やcloudinaryに指定し、表示する際もそこから参照できるようにするにはどのようにすれば良いでしょうか?
|
9
|
-
初心者なので見当違いの質問でしたらすみません。
|
9
|
+
初心者なので見当違いの質問でしたらすみません。
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class PostsController < ApplicationController
|
13
|
+
|
14
|
+
before_action :authenticate_user
|
15
|
+
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
|
16
|
+
|
17
|
+
def index
|
18
|
+
@posts = Post.all.order(created_at: :desc)
|
19
|
+
end
|
20
|
+
|
21
|
+
def show
|
22
|
+
@post = Post.find_by(id: params[:id])
|
23
|
+
@user = @post.user
|
24
|
+
@likes_count = Like.where(post_id: @post.id).count
|
25
|
+
@comment = Comment.find_by(id: params[:id])
|
26
|
+
end
|
27
|
+
|
28
|
+
def new
|
29
|
+
@post = Post.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def create
|
33
|
+
|
34
|
+
@post = Post.new(content:params[:content],
|
35
|
+
user_id: @current_user.id,
|
36
|
+
)
|
37
|
+
@post.save
|
38
|
+
|
39
|
+
@user = User.find(@current_user.id)
|
40
|
+
@user.user_exp += 5
|
41
|
+
@user.save
|
42
|
+
|
43
|
+
if params[:image]
|
44
|
+
@post.image_name = "#{@post.id}.jpg"
|
45
|
+
image = params[:image]
|
46
|
+
File.binwrite("public/post_images/#{@post.image_name}",image.read)
|
47
|
+
end
|
48
|
+
|
49
|
+
if @post.save
|
50
|
+
redirect_to("/posts/index")
|
51
|
+
else
|
52
|
+
render("posts/new")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def edit
|
57
|
+
@post = Post.find_by(id: params[:id])
|
58
|
+
end
|
59
|
+
|
60
|
+
def update
|
61
|
+
@post = Post.find_by(id: params[:id])
|
62
|
+
@post.content = params[:content]
|
63
|
+
if @post.save
|
64
|
+
redirect_to("/posts/index")
|
65
|
+
else
|
66
|
+
render("posts/edit")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def destroy
|
71
|
+
@post = Post.find_by(id: params[:id])
|
72
|
+
@post.destroy
|
73
|
+
redirect_to("/posts/index")
|
74
|
+
|
75
|
+
@user = User.find(@current_user.id)
|
76
|
+
@user.user_exp -= 5
|
77
|
+
@user.save
|
78
|
+
end
|
79
|
+
|
80
|
+
def ensure_correct_user
|
81
|
+
@post = Post.find_by(id: params[:id])
|
82
|
+
if @post.user_id != @current_user.id
|
83
|
+
flash[:notice] = "権限がありません"
|
84
|
+
redirect_to("/posts/index")
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
```
|
93
|
+
```ruby
|
94
|
+
<div class="main posts-new">
|
95
|
+
<div class="container">
|
96
|
+
<h1 class="form-heading">New Post</h1>
|
97
|
+
|
98
|
+
<%= form_tag("/posts/create",{multipart:true}) do %>
|
99
|
+
<div class="form">
|
100
|
+
<div class="form-body">
|
101
|
+
<% @post.errors.full_messages.each do |message| %>
|
102
|
+
|
103
|
+
<div class="form-error">
|
104
|
+
<%= message %>
|
105
|
+
</div>
|
106
|
+
<% end %>
|
107
|
+
<textarea name="content"><%= @post.content %></textarea>
|
108
|
+
<p>画像</p>
|
109
|
+
<input name="image" type="file">
|
110
|
+
<input type="submit" value="Post">
|
111
|
+
</div>
|
112
|
+
</div>
|
113
|
+
<% end %>
|
114
|
+
</div>
|
115
|
+
</div>
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
<div class="main posts-show">
|
121
|
+
<div class="container">
|
122
|
+
<div class="posts-show-item">
|
123
|
+
<div class="post-user-name">
|
124
|
+
|
125
|
+
<img src="<%= "/user_images/#{@user.image_name}" %>">
|
126
|
+
<%= link_to(@user.name, "/users/#{@user.id}") %>
|
127
|
+
|
128
|
+
</div>
|
129
|
+
<p>
|
130
|
+
<%= @post.content %>
|
131
|
+
</p>
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
<%= @post.created_at %>
|
136
|
+
|
137
|
+
|
138
|
+
<% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %>
|
139
|
+
|
140
|
+
<%= link_to( "/likes/#{@post.id}/destroy", {method: "post"}) do %>
|
141
|
+
<span class="fa fa-heart like-btn-unlike"></span>
|
142
|
+
<% end %>
|
143
|
+
|
144
|
+
<% else %>
|
145
|
+
<%= link_to( "/likes/#{@post.id}/create", {method: "post"}) do %>
|
146
|
+
<span class="fa fa-heart like-btn"></span>
|
147
|
+
<% end %>
|
148
|
+
|
149
|
+
<% end %>
|
150
|
+
<%= @likes_count %>
|
151
|
+
|
152
|
+
<% if @post.user_id == @current_user.id %>
|
153
|
+
<div class="post-menus">
|
154
|
+
<%= link_to("編集", "/posts/#{@post.id}/edit") %>
|
155
|
+
<%= link_to("削除", "/posts/#{@post.id}/destroy",{method:"post"}) %>
|
156
|
+
</div>
|
157
|
+
<% end %>
|
158
|
+
</div>
|
159
|
+
|
160
|
+
<% if @post.image_name != nil %>
|
161
|
+
<p class="post_image">
|
162
|
+
<img width="570px" src="<%= "/post_images/#{@post.image_name}" %>">
|
163
|
+
</p>
|
164
|
+
<% end %>
|
165
|
+
|
166
|
+
<div class ="post_comment">
|
167
|
+
<%= form_tag("/comments/#{@post.id}/create",{multipart:true}) do %>
|
168
|
+
<textarea name="content" cols=50 rows=10 ></textarea>
|
169
|
+
<input type="submit" value="Encourage!!!">
|
170
|
+
</div>
|
171
|
+
<% end %>
|
172
|
+
```
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
class Post < ApplicationRecord
|
176
|
+
validates :content, {presence: true,length:{maximum:140}}
|
177
|
+
validates :user_id, {presence: true}
|
178
|
+
|
179
|
+
def user
|
180
|
+
return User.find_by(id: self.user_id)
|
181
|
+
end
|
182
|
+
|
183
|
+
def comments
|
184
|
+
return Comment.where(post_id: self.id)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
```
|