質問編集履歴

3

情報の追加

2020/08/27 10:24

投稿

75ks
75ks

スコア4

test CHANGED
File without changes
test CHANGED
@@ -68,6 +68,234 @@
68
68
 
69
69
 
70
70
 
71
+ ```model
72
+
73
+ class Post < ApplicationRecord
74
+
75
+ has_one_attached :image
76
+
77
+ belongs_to :user
78
+
79
+
80
+
81
+ with_options presence: true do
82
+
83
+ validates :text
84
+
85
+ validates :image
86
+
87
+ validates :user
88
+
89
+ end
90
+
91
+ end
92
+
93
+
94
+
95
+ ```
96
+
97
+
98
+
99
+ ```controller
100
+
101
+ class PostsController < ApplicationController
102
+
103
+ before_action :set_post, only: [:show, :destroy, :edit, :update]
104
+
105
+ before_action :correct_user, only: [:edit, :update]
106
+
107
+
108
+
109
+ def index
110
+
111
+ @posts = Post.all.order("created_at DESC")
112
+
113
+ end
114
+
115
+
116
+
117
+ def new
118
+
119
+ @post = Post.new
120
+
121
+ end
122
+
123
+
124
+
125
+ def create
126
+
127
+ @post = Post.new(post_params)
128
+
129
+ if @post.valid?
130
+
131
+ @post.save
132
+
133
+ flash[:notice] = "投稿が完了しました"
134
+
135
+ redirect_to root_path
136
+
137
+ else
138
+
139
+ flash.now[:alert] = "投稿に失敗しました"
140
+
141
+ render :new
142
+
143
+ end
144
+
145
+ end
146
+
147
+
148
+
149
+ def show
150
+
151
+ end
152
+
153
+
154
+
155
+ def destroy
156
+
157
+ if @post.destroy
158
+
159
+ flash[:notice] = "削除が完了しました"
160
+
161
+ redirect_to root_path
162
+
163
+ else
164
+
165
+ flash.now[:alert] = "削除に失敗しました"
166
+
167
+ render :show
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+ def edit
176
+
177
+ end
178
+
179
+
180
+
181
+ def update
182
+
183
+ if @post.update(post_params)
184
+
185
+ flash[:notice] = "編集が完了しました"
186
+
187
+ redirect_to post_path(@post.id)
188
+
189
+ else
190
+
191
+ flash.now[:alert] = "編集に失敗しました"
192
+
193
+ render :edit
194
+
195
+ end
196
+
197
+ end
198
+
199
+
200
+
201
+ private
202
+
203
+
204
+
205
+ def post_params
206
+
207
+ params.require(:post).permit(:text, :image).merge(user_id: current_user.id)
208
+
209
+ end
210
+
211
+
212
+
213
+ def set_post
214
+
215
+ @post = Post.find(params[:id])
216
+
217
+ end
218
+
219
+
220
+
221
+ def correct_user
222
+
223
+ unless user_signed_in? && current_user.id == @post.user.id
224
+
225
+ redirect_to root_path
226
+
227
+ end
228
+
229
+ end
230
+
231
+ end
232
+
233
+
234
+
235
+ ```
236
+
237
+
238
+
239
+ ```viewindex
240
+
241
+ <%= render "shared/header"%>
242
+
243
+
244
+
245
+ <div class="main">
246
+
247
+ <div class="post-contents">
248
+
249
+ <ul class="post-lists">
250
+
251
+ <% @posts.each do |post| %>
252
+
253
+ <li class="list">
254
+
255
+ <span>@<%= post.user.nickname %></span>
256
+
257
+ <%= link_to post_path(post.id) do %>
258
+
259
+ <%= image_tag post.image.variant(resize:'500x500'), class:"post-image" %>
260
+
261
+ <% end %>
262
+
263
+ <div class="heart-btn">
264
+
265
+ <%= image_tag "heart.png", class:"heart-icon" %>
266
+
267
+ <span class="heart-count">0</span>
268
+
269
+ </div>
270
+
271
+ <p class="post-text">
272
+
273
+ <%= post.text %>
274
+
275
+ </p>
276
+
277
+ </li>
278
+
279
+ <% end %>
280
+
281
+ </ul>
282
+
283
+ </div>
284
+
285
+ </div>
286
+
287
+ <%= link_to new_post_path do %>
288
+
289
+ <div class="new-post-btn btn">
290
+
291
+ <span class="new-post-btn-text btn-text">投稿</span>
292
+
293
+ </div>
294
+
295
+ <% end %>
296
+
297
+ ```
298
+
71
299
 
72
300
 
73
301
 

2

URLを削除

2020/08/27 10:24

投稿

75ks
75ks

スコア4

test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  2. ターミナルでEC2にログインし、以下を実行
20
20
 
21
- ```ターミナル
21
+ ```terminal
22
22
 
23
23
  rails db:drop RAILS_ENV=production DISABLE_DATABASE_ENVIRONMENT_CHECK=1
24
24
 
@@ -32,19 +32,41 @@
32
32
 
33
33
  3.ターミナル(ローカル)で以下を実行
34
34
 
35
- ```ターミナル
35
+ ```terminal
36
36
 
37
37
  bundle exec cap production deploy
38
38
 
39
39
  ```
40
40
 
41
+ 以下は`less shared/log/production.log`を実行したときの最新のログです。
42
+
43
+ ```terminal
44
+
45
+ I, [2020-08-27T08:58:29.351338 #1720] INFO -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370] Started POST "/posts" for 131.147.51.47 at 2020-08-27 08:58:29 +0000
46
+
47
+ I, [2020-08-27T08:58:29.351988 #1720] INFO -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370] Processing by PostsController#create as HTML
48
+
49
+ I, [2020-08-27T08:58:29.352071 #1720] INFO -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370] Parameters: {"authenticity_token"=>"2yJwSfXk9yKSODwhcC5EWygFo/oBKS9azS0OMrtC/LnTYN6/9YAz+U3tHCN+fFOhX7Um+NyJIpJzkt6Mzaz0PQ==", "post"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x00000000074dc680 @tempfile=#<Tempfile:/tmp/RackMultipart20200827-1720-1n69mmi.png>, @original_filename="cap.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[image]\"; filename=\"cap.png\"\r\nContent-Type: image/png\r\n">, "text"=>"test"}, "commit"=>"投稿する"}
50
+
51
+ D, [2020-08-27T08:58:29.353627 #1720] DEBUG -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370] ESC[1mESC[36mUser Load (0.4ms)ESC[0m ESC[1mESC[34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1ESC[0m
52
+
53
+ I, [2020-08-27T08:58:29.354283 #1720] INFO -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370] Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.4ms | Allocations: 778)
54
+
55
+ F, [2020-08-27T08:58:29.354723 #1720] FATAL -- : [5d2cfbac-f188-4547-bb8d-b34e1b247370]
56
+
57
+ [5d2cfbac-f188-4547-bb8d-b34e1b247370] ActiveModel::UnknownAttributeError (unknown attribute 'user_id' for Post.):
58
+
59
+ [5d2cfbac-f188-4547-bb8d-b34e1b247370]
60
+
61
+ [5d2cfbac-f188-4547-bb8d-b34e1b247370] app/controllers/posts_controller.rb:14:in `create'
62
+
63
+ (END)
41
64
 
42
65
 
43
- ログはこちらになります。
44
66
 
45
- [shared/log/unicorn.stderr.log](https://gyazo.com/60b8035c31d1d2081f99756a2a7e17d7)
67
+ ```
46
68
 
47
- [shared/log/production.log](https://gyazo.com/cb8f83c3887e594ee328a5ea7d2aeefa)
69
+
48
70
 
49
71
 
50
72
 

1

リンクを貼った。

2020/08/27 09:03

投稿

75ks
75ks

スコア4

test CHANGED
File without changes
test CHANGED
@@ -42,17 +42,9 @@
42
42
 
43
43
  ログはこちらになります。
44
44
 
45
- `shared/log/unicorn.stderr.log`
45
+ [shared/log/unicorn.stderr.log](https://gyazo.com/60b8035c31d1d2081f99756a2a7e17d7)
46
46
 
47
- https://gyazo.com/60b8035c31d1d2081f99756a2a7e17d7
48
-
49
-
50
-
51
-
52
-
53
- `shared/log/production.log`
54
-
55
- https://gyazo.com/cb8f83c3887e594ee328a5ea7d2aeefa
47
+ [shared/log/production.log](https://gyazo.com/cb8f83c3887e594ee328a5ea7d2aeefa)
56
48
 
57
49
 
58
50