質問編集履歴

1

誤字

2020/11/16 05:33

投稿

ocs_R
ocs_R

スコア6

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,11 @@
14
14
 
15
15
 
16
16
 
17
- ```
17
+
18
-
18
+
19
- ![イメージ説明](b9ecbfd57773de5ceb22587f48224b8d.png)
19
+ ![イメージ説明](d389268861383a154b4b8a85dfbf6573.png)
20
-
21
- ```
20
+
21
+
22
22
 
23
23
 
24
24
 
@@ -26,204 +26,204 @@
26
26
 
27
27
 
28
28
 
29
+ ```
30
+
31
+ #app/views/posts/new.html.erb
32
+
33
+ <div class="main posts-new">
34
+
35
+ <div class="container">
36
+
37
+ <h1 class="form-heading">投稿する</h1>
38
+
39
+ <%= form_tag("/posts/create",{multipart:true}) do %>
40
+
41
+ <div>
42
+
43
+
44
+
45
+ <textarea name="content"></textarea><br>
46
+
47
+ <%= f.label :image %>
48
+
49
+ <%= f.file_field :image %>
50
+
51
+
52
+
53
+ </div>
54
+
55
+ <%end%>
56
+
57
+ ```
58
+
29
59
  ```ここに言語名を入力
30
60
 
61
+ #app\controllers\posts_controller.rb
62
+
63
+ class PostsController < ApplicationController
64
+
65
+ before_action :authenticate_user
66
+
67
+ before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
68
+
69
+
70
+
71
+ def post_permit
72
+
73
+ params.permit(:id, :name, :image)
74
+
75
+ end
76
+
77
+
78
+
79
+ def index
80
+
81
+ @posts = Post.all.order(created_at: :desc)
82
+
83
+ end
84
+
85
+
86
+
87
+ def show
88
+
89
+ @post = Post.find_by(id: params[:id])
90
+
91
+ @user = @post.user
92
+
93
+ # 変数@likes_countを定義してください
94
+
95
+ @likes_count = Like.where(post_id: @post.id).count
96
+
97
+ end
98
+
99
+
100
+
101
+ def new
102
+
103
+ @post = Post.new
104
+
105
+ end
106
+
107
+
108
+
109
+ def create
110
+
111
+ @post = Post.new(
112
+
113
+ content: params[:content],
114
+
115
+ user_id: @current_user.id,
116
+
117
+ post_image: nil)
118
+
119
+ @post.save
120
+
121
+
122
+
123
+ if @post.save
124
+
125
+
126
+
127
+ if params[:post_image]
128
+
129
+ @post.post_image = "#{@post.id}.jpg"
130
+
131
+ image = params[:post_image]
132
+
133
+ File.binwrite("public/post_images/#{@post.post_image}",image.read)
134
+
135
+ end
136
+
137
+
138
+
139
+ flash[:notice] = "投稿を作成しました"
140
+
141
+ redirect_to("/posts/index")
142
+
143
+ else
144
+
145
+ render("posts/new")
146
+
147
+ end
148
+
149
+ end
150
+
151
+
152
+
153
+
154
+
155
+ def edit
156
+
157
+ @post = Post.find_by(id: params[:id])
158
+
159
+ end
160
+
161
+
162
+
163
+ def update
164
+
165
+ @post = Post.find_by(id: params[:id])
166
+
167
+ @post.content = params[:content]
168
+
169
+ if @post.save
170
+
171
+ flash[:notice] = "投稿を編集しました"
172
+
173
+ redirect_to("/posts/index")
174
+
175
+ else
176
+
177
+ render("posts/edit")
178
+
179
+ end
180
+
181
+ end
182
+
183
+
184
+
185
+ def destroy
186
+
187
+ @post = Post.find_by(id: params[:id])
188
+
189
+ @post.destroy
190
+
191
+ flash[:notice] = "投稿を削除しました"
192
+
193
+ redirect_to("/posts/index")
194
+
195
+ end
196
+
197
+
198
+
199
+ def ensure_correct_user
200
+
201
+ @post = Post.find_by(id: params[:id])
202
+
203
+ if @post.user_id != @current_user.id
204
+
205
+ flash[:notice] = "権限がありません"
206
+
207
+ redirect_to("/posts/index")
208
+
209
+ end
210
+
211
+ end
212
+
213
+
214
+
215
+ end
216
+
217
+
218
+
219
+ ```
220
+
221
+ ```ここに言語名を入力
222
+
31
- #app/views/posts/new.html.erb
223
+ #app\views\posts\index.html.erb
32
-
33
- <div class="main posts-new">
34
224
 
35
225
  <div class="container">
36
226
 
37
- <h1 class="form-heading">投稿する</h1>
38
-
39
- <%= form_tag("/posts/create",{multipart:true}) do %>
40
-
41
- <div>
42
-
43
-
44
-
45
- <textarea name="content"></textarea><br>
46
-
47
- <%= f.label :image %>
48
-
49
- <%= f.file_field :image %>
50
-
51
-
52
-
53
- </div>
54
-
55
- <%end%>
56
-
57
- ```
58
-
59
- ```ここに言語名を入力
60
-
61
- #app\controllers\posts_controller.rb
62
-
63
- class PostsController < ApplicationController
64
-
65
- before_action :authenticate_user
66
-
67
- before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
68
-
69
-
70
-
71
- def post_permit
72
-
73
- params.permit(:id, :name, :image)
74
-
75
- end
76
-
77
-
78
-
79
- def index
80
-
81
- @posts = Post.all.order(created_at: :desc)
82
-
83
- end
84
-
85
-
86
-
87
- def show
88
-
89
- @post = Post.find_by(id: params[:id])
90
-
91
- @user = @post.user
92
-
93
- # 変数@likes_countを定義してください
94
-
95
- @likes_count = Like.where(post_id: @post.id).count
96
-
97
- end
98
-
99
-
100
-
101
- def new
102
-
103
- @post = Post.new
104
-
105
- end
106
-
107
-
108
-
109
- def create
110
-
111
- @post = Post.new(
112
-
113
- content: params[:content],
114
-
115
- user_id: @current_user.id,
116
-
117
- post_image: nil)
118
-
119
- @post.save
120
-
121
-
122
-
123
- if @post.save
124
-
125
-
126
-
127
- if params[:post_image]
128
-
129
- @post.post_image = "#{@post.id}.jpg"
130
-
131
- image = params[:post_image]
132
-
133
- File.binwrite("public/post_images/#{@post.post_image}",image.read)
134
-
135
- end
136
-
137
-
138
-
139
- flash[:notice] = "投稿を作成しました"
140
-
141
- redirect_to("/posts/index")
142
-
143
- else
144
-
145
- render("posts/new")
146
-
147
- end
148
-
149
- end
150
-
151
-
152
-
153
-
154
-
155
- def edit
156
-
157
- @post = Post.find_by(id: params[:id])
158
-
159
- end
160
-
161
-
162
-
163
- def update
164
-
165
- @post = Post.find_by(id: params[:id])
166
-
167
- @post.content = params[:content]
168
-
169
- if @post.save
170
-
171
- flash[:notice] = "投稿を編集しました"
172
-
173
- redirect_to("/posts/index")
174
-
175
- else
176
-
177
- render("posts/edit")
178
-
179
- end
180
-
181
- end
182
-
183
-
184
-
185
- def destroy
186
-
187
- @post = Post.find_by(id: params[:id])
188
-
189
- @post.destroy
190
-
191
- flash[:notice] = "投稿を削除しました"
192
-
193
- redirect_to("/posts/index")
194
-
195
- end
196
-
197
-
198
-
199
- def ensure_correct_user
200
-
201
- @post = Post.find_by(id: params[:id])
202
-
203
- if @post.user_id != @current_user.id
204
-
205
- flash[:notice] = "権限がありません"
206
-
207
- redirect_to("/posts/index")
208
-
209
- end
210
-
211
- end
212
-
213
-
214
-
215
- end
216
-
217
-
218
-
219
- ```
220
-
221
- ```ここに言語名を入力
222
-
223
- #app\views\posts\index.html.erb
224
-
225
- <div class="container">
226
-
227
227
  <% @posts.each do |post| %>
228
228
 
229
229
  <div class="posts-index-item">