質問編集履歴

3

プログラムを```で囲みました。

2020/03/20 03:02

投稿

KosukeYamamoto
KosukeYamamoto

スコア220

test CHANGED
File without changes
test CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  comments_controller.rb
62
62
 
63
-
63
+ ```
64
64
 
65
65
  class CommentsController < ApplicationController
66
66
 
@@ -110,10 +110,14 @@
110
110
 
111
111
  end
112
112
 
113
+ ```
114
+
113
115
 
114
116
 
115
117
  posts_controller.rb
116
118
 
119
+ ```
120
+
117
121
 
118
122
 
119
123
  class PostsController < ApplicationController
@@ -296,10 +300,14 @@
296
300
 
297
301
  end
298
302
 
303
+ ```
304
+
299
305
 
300
306
 
301
307
  show.html.erb
302
308
 
309
+ ```
310
+
303
311
 
304
312
 
305
313
  <div class="container">
@@ -610,6 +618,8 @@
610
618
 
611
619
  </div>
612
620
 
621
+ ```
622
+
613
623
 
614
624
 
615
625
  sqlite> select * from likes;

2

テスト

2020/03/20 03:02

投稿

KosukeYamamoto
KosukeYamamoto

スコア220

test CHANGED
File without changes
test CHANGED
@@ -24,9 +24,9 @@
24
24
 
25
25
  likes_controller.rb
26
26
 
27
-
27
+ ```
28
-
28
+
29
- ```class LikesController < ApplicationController
29
+ class LikesController < ApplicationController
30
30
 
31
31
  def create
32
32
 
@@ -52,7 +52,9 @@
52
52
 
53
53
  end
54
54
 
55
- end```
55
+ end
56
+
57
+ ```
56
58
 
57
59
 
58
60
 

1

テスト

2020/03/20 03:01

投稿

KosukeYamamoto
KosukeYamamoto

スコア220

test CHANGED
File without changes
test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
 
28
28
 
29
- class LikesController < ApplicationController
29
+ ```class LikesController < ApplicationController
30
30
 
31
31
  def create
32
32
 
@@ -52,250 +52,250 @@
52
52
 
53
53
  end
54
54
 
55
+ end```
56
+
57
+
58
+
59
+ comments_controller.rb
60
+
61
+
62
+
63
+ class CommentsController < ApplicationController
64
+
65
+ def create
66
+
67
+ @post = Post.find(params[:post_id])
68
+
69
+ @post.comments.create(comment_params.merge(user_id: current_user.id))
70
+
71
+ redirect_to post_path(@post)
72
+
73
+ end
74
+
75
+
76
+
77
+ def destroy
78
+
79
+ @post = Post.find(params[:post_id])
80
+
81
+ @comment = @post.comments.find(params[:id])
82
+
83
+ @comment.destroy
84
+
85
+ redirect_to post_path(@post)
86
+
87
+ end
88
+
89
+
90
+
91
+ private
92
+
93
+ def comment_params
94
+
95
+ params.require(:comment).permit(:body)
96
+
97
+ end
98
+
99
+ # Use callbacks to share common setup or constraints between actions.
100
+
101
+ def set_comment
102
+
103
+ @post.comments = Comment.find(params[:id])
104
+
105
+ end
106
+
107
+
108
+
55
109
  end
56
110
 
57
111
 
58
112
 
59
- comments_controller.rb
60
-
61
-
62
-
63
- class CommentsController < ApplicationController
64
-
65
- def create
66
-
67
- @post = Post.find(params[:post_id])
68
-
69
- @post.comments.create(comment_params.merge(user_id: current_user.id))
70
-
71
- redirect_to post_path(@post)
72
-
73
- end
74
-
75
-
76
-
77
- def destroy
78
-
79
- @post = Post.find(params[:post_id])
80
-
81
- @comment = @post.comments.find(params[:id])
82
-
83
- @comment.destroy
84
-
85
- redirect_to post_path(@post)
86
-
87
- end
88
-
89
-
90
-
91
- private
92
-
93
- def comment_params
94
-
95
- params.require(:comment).permit(:body)
96
-
97
- end
98
-
99
- # Use callbacks to share common setup or constraints between actions.
100
-
101
- def set_comment
102
-
103
- @post.comments = Comment.find(params[:id])
104
-
105
- end
106
-
107
-
113
+ posts_controller.rb
114
+
115
+
116
+
117
+ class PostsController < ApplicationController
118
+
119
+ before_action :require_login, except: [:index, :show]
120
+
121
+ before_action :set_post, only: [:show, :edit, :update, :destroy]
122
+
123
+ before_action :ensure_correct_user,{only: [:edit,:update,:destroy]}
124
+
125
+ # カレントユーザー以外消せなくする
126
+
127
+
128
+
129
+ # GET /posts
130
+
131
+ # GET /posts.json
132
+
133
+ def index
134
+
135
+ @posts = Post.all.order('updated_at DESC')
136
+
137
+ @post = Post.all.order('updated_at DESC').select { |x| x["counter"] > 0 }
138
+
139
+ if params[:title].present?
140
+
141
+ @posts = @posts.get_by_title params[:title]
142
+
143
+ elsif params[:genre] then
144
+
145
+ @posts = @posts.get_by_genre params[:genre]
146
+
147
+ elsif params[:except] then
148
+
149
+ @posts = @posts.where.not(:genre => params[:except])
150
+
151
+ end
152
+
153
+
154
+
155
+ @genres = Genre.all
156
+
157
+ end
158
+
159
+
160
+
161
+ # GET /posts/1
162
+
163
+ # GET /posts/1.json
164
+
165
+ def show
166
+
167
+ @same = Post.all.select { |x| x["title"] == @post.title }
168
+
169
+ @same.delete_if { |h| h["id"] == @post.id}
170
+
171
+ @similar= Post.all.select { |x| x["genre"] == @post.genre }
172
+
173
+ @similar.delete_if { |h| h["id"] == @post.id}
174
+
175
+ @like = Like.new
176
+
177
+ end
178
+
179
+
180
+
181
+ # GET /posts/new
182
+
183
+ def new
184
+
185
+ @post = Post.new
186
+
187
+ end
188
+
189
+
190
+
191
+ # GET /posts/1/edit
192
+
193
+ def edit
194
+
195
+ end
196
+
197
+
198
+
199
+ # POST /posts
200
+
201
+ # POST /posts.json
202
+
203
+ def create
204
+
205
+ @post = Post.new(post_params)
206
+
207
+ @post.user = current_user
208
+
209
+
210
+
211
+ respond_to do |format|
212
+
213
+ if @post.save
214
+
215
+ format.html { redirect_to @post, notice: 'Post was successfully created.' }
216
+
217
+ format.json { render :show, status: :created, location: @post }
218
+
219
+ else
220
+
221
+ format.html { render :new }
222
+
223
+ format.json { render json: @post.errors, status: :unprocessable_entity }
224
+
225
+ end
226
+
227
+ end
228
+
229
+ end
230
+
231
+ # PATCH/PUT /posts/1
232
+
233
+ # PATCH/PUT /posts/1.json
234
+
235
+ def update
236
+
237
+ if params[:promote] then
238
+
239
+ @post.increment!(:counter, by = 1)
240
+
241
+ @post.touch
242
+
243
+ redirect_to @post
244
+
245
+ else
246
+
247
+ @post.update(post_params)
248
+
249
+ redirect_to @post
250
+
251
+ end
252
+
253
+ end
254
+
255
+ # DELETE /posts/1
256
+
257
+ # DELETE /posts/1.json
258
+
259
+ def destroy
260
+
261
+ @post.destroy
262
+
263
+ respond_to do |format|
264
+
265
+ format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
266
+
267
+ format.json { head :no_content }
268
+
269
+ end
270
+
271
+ end
272
+
273
+
274
+
275
+ private
276
+
277
+ # Use callbacks to share common setup or constraints between actions.
278
+
279
+ def set_post
280
+
281
+ @post = Post.find(params[:id])
282
+
283
+ end
284
+
285
+
286
+
287
+ # Never trust parameters from the scary internet, only allow the white list through.
288
+
289
+ def post_params
290
+
291
+ params.require(:post).permit(:content, :title, :genre, :price, :location, {image: []}, :channel, :status, :counter, :link, :situation)
292
+
293
+ end
108
294
 
109
295
  end
110
296
 
111
297
 
112
298
 
113
- posts_controller.rb
114
-
115
-
116
-
117
- class PostsController < ApplicationController
118
-
119
- before_action :require_login, except: [:index, :show]
120
-
121
- before_action :set_post, only: [:show, :edit, :update, :destroy]
122
-
123
- before_action :ensure_correct_user,{only: [:edit,:update,:destroy]}
124
-
125
- # カレントユーザー以外消せなくする
126
-
127
-
128
-
129
- # GET /posts
130
-
131
- # GET /posts.json
132
-
133
- def index
134
-
135
- @posts = Post.all.order('updated_at DESC')
136
-
137
- @post = Post.all.order('updated_at DESC').select { |x| x["counter"] > 0 }
138
-
139
- if params[:title].present?
140
-
141
- @posts = @posts.get_by_title params[:title]
142
-
143
- elsif params[:genre] then
144
-
145
- @posts = @posts.get_by_genre params[:genre]
146
-
147
- elsif params[:except] then
148
-
149
- @posts = @posts.where.not(:genre => params[:except])
150
-
151
- end
152
-
153
-
154
-
155
- @genres = Genre.all
156
-
157
- end
158
-
159
-
160
-
161
- # GET /posts/1
162
-
163
- # GET /posts/1.json
164
-
165
- def show
166
-
167
- @same = Post.all.select { |x| x["title"] == @post.title }
168
-
169
- @same.delete_if { |h| h["id"] == @post.id}
170
-
171
- @similar= Post.all.select { |x| x["genre"] == @post.genre }
172
-
173
- @similar.delete_if { |h| h["id"] == @post.id}
174
-
175
- @like = Like.new
176
-
177
- end
178
-
179
-
180
-
181
- # GET /posts/new
182
-
183
- def new
184
-
185
- @post = Post.new
186
-
187
- end
188
-
189
-
190
-
191
- # GET /posts/1/edit
192
-
193
- def edit
194
-
195
- end
196
-
197
-
198
-
199
- # POST /posts
200
-
201
- # POST /posts.json
202
-
203
- def create
204
-
205
- @post = Post.new(post_params)
206
-
207
- @post.user = current_user
208
-
209
-
210
-
211
- respond_to do |format|
212
-
213
- if @post.save
214
-
215
- format.html { redirect_to @post, notice: 'Post was successfully created.' }
216
-
217
- format.json { render :show, status: :created, location: @post }
218
-
219
- else
220
-
221
- format.html { render :new }
222
-
223
- format.json { render json: @post.errors, status: :unprocessable_entity }
224
-
225
- end
226
-
227
- end
228
-
229
- end
230
-
231
- # PATCH/PUT /posts/1
232
-
233
- # PATCH/PUT /posts/1.json
234
-
235
- def update
236
-
237
- if params[:promote] then
238
-
239
- @post.increment!(:counter, by = 1)
240
-
241
- @post.touch
242
-
243
- redirect_to @post
244
-
245
- else
246
-
247
- @post.update(post_params)
248
-
249
- redirect_to @post
250
-
251
- end
252
-
253
- end
254
-
255
- # DELETE /posts/1
256
-
257
- # DELETE /posts/1.json
258
-
259
- def destroy
260
-
261
- @post.destroy
262
-
263
- respond_to do |format|
264
-
265
- format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
266
-
267
- format.json { head :no_content }
268
-
269
- end
270
-
271
- end
272
-
273
-
274
-
275
- private
276
-
277
- # Use callbacks to share common setup or constraints between actions.
278
-
279
- def set_post
280
-
281
- @post = Post.find(params[:id])
282
-
283
- end
284
-
285
-
286
-
287
- # Never trust parameters from the scary internet, only allow the white list through.
288
-
289
- def post_params
290
-
291
- params.require(:post).permit(:content, :title, :genre, :price, :location, {image: []}, :channel, :status, :counter, :link, :situation)
292
-
293
- end
294
-
295
- end
296
-
297
-
298
-
299
299
  show.html.erb
300
300
 
301
301