質問編集履歴

2

v

2020/10/09 12:03

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -353,3 +353,103 @@
353
353
  <%=f.submit value: "SENT",class: "game_record"%>
354
354
 
355
355
  ```
356
+
357
+
358
+
359
+ tweet.js
360
+
361
+ ```
362
+
363
+ $(document).on('turbolinks:load', (function(){
364
+
365
+ function appendOption(category) {
366
+
367
+ let html =
368
+
369
+ `<option value="${category.id}" data-category="${category.id}">${category.name}</option>`;
370
+
371
+ return html;
372
+
373
+ }
374
+
375
+ function appendChildrenBox(insertHTML) {
376
+
377
+ let childSelectHtml = '';
378
+
379
+ childSelectHtml =
380
+
381
+ `<p>高校A</p>
382
+
383
+ <select name="tweet[school_a_id]" class="tournament_select_child" id="children_category">
384
+
385
+ <option value="" data-category="" >学校を選択してください</option>
386
+
387
+ ${insertHTML}</select>`
388
+
389
+ $(".school").append(childSelectHtml)
390
+
391
+ childSelectHtml2 =
392
+
393
+ `<p>高校B</p>
394
+
395
+ <select name="tweet[school_b_id]" class="tournament_select_child" id="children_category">
396
+
397
+ <option value="" data-category="" >学校を選択してください</option>
398
+
399
+ ${insertHTML}</select>`
400
+
401
+ $(".school2").append(childSelectHtml2)
402
+
403
+ }
404
+
405
+
406
+
407
+ $("#parent_category").on("change",function(){
408
+
409
+ let parentCategory = $("#parent_category").val();
410
+
411
+ if(parentCategory !=""){
412
+
413
+ $.ajax({
414
+
415
+ type: 'GET',
416
+
417
+ url: '/tweets/new',
418
+
419
+ data: {tournament_id: parentCategory},
420
+
421
+ dataType: 'json'
422
+
423
+ })
424
+
425
+ .done(function(children){
426
+
427
+ $(".school").empty();
428
+
429
+ $(".school2").empty();
430
+
431
+ let insertHTML = '';
432
+
433
+ children.forEach(function(child) {
434
+
435
+ insertHTML += appendOption(child);
436
+
437
+ });
438
+
439
+ appendChildrenBox(insertHTML);
440
+
441
+ })
442
+
443
+
444
+
445
+ .fail(function() {
446
+
447
+ alert('error:子カテゴリーの取得に失敗');
448
+
449
+ })
450
+
451
+ }
452
+
453
+ });
454
+
455
+ ```

1

f

2020/10/09 12:02

投稿

Meitoku
Meitoku

スコア44

test CHANGED
File without changes
test CHANGED
@@ -49,3 +49,307 @@
49
49
  @category = Category.find(params[:id])
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ ####追記
56
+
57
+
58
+
59
+ ######簡単な実装の説明
60
+
61
+ 多階層のカテゴリを実装しています
62
+
63
+ 現在取得したいidというのは親カテゴリのidです
64
+
65
+ 以下で親カテゴリのidを取得して、childrenメソッドで子カテゴリのidを取得できるのかなと思っています
66
+
67
+ ```
68
+
69
+ def edit
70
+
71
+ @category_children = Category.find(params[:id]).children
72
+
73
+ end
74
+
75
+ ```
76
+
77
+
78
+
79
+ そしてviewファイルで子カテゴリを全てセレクトボックスで表示させようかと思っています
80
+
81
+ ```
82
+
83
+ <%=f.select :school_a_id,options_for_select(@category_children.map{|c| [c[:name], c[:id]]},selected: @tweet.tournament_id),{},{class: "tournament_select_child",id: "children_category"} %>
84
+
85
+ ```
86
+
87
+
88
+
89
+ ###各ファイル
90
+
91
+
92
+
93
+ tweetモデル
94
+
95
+
96
+
97
+ ```
98
+
99
+ class Tweet < ApplicationRecord
100
+
101
+
102
+
103
+ validates :title_info,length: {maximum: 30}
104
+
105
+ validates :tournament_id,:school_a,:school_b,:school_a_score,:school_b_score,:text,:title_info ,presence: true
106
+
107
+ mount_uploader :image, ImageUploader
108
+
109
+ belongs_to :user
110
+
111
+ has_many :tournaments
112
+
113
+ has_many :categories
114
+
115
+ has_many :liked_users,through: :likes,source: :user
116
+
117
+ belongs_to :school_a,class_name: 'Category', foreign_key: 'school_a_id'
118
+
119
+ belongs_to :school_b,class_name: 'Category', foreign_key: 'school_b_id'
120
+
121
+ end
122
+
123
+ ```
124
+
125
+
126
+
127
+ categoryモデル
128
+
129
+
130
+
131
+ ```
132
+
133
+ class Category < ApplicationRecord
134
+
135
+ has_many :tweets
136
+
137
+ has_ancestry
138
+
139
+
140
+
141
+ has_many :school_a_tweets,class_name: 'Tweet', foreign_key: 'school_a_id'
142
+
143
+ has_many :school_b_tweets,class_name: 'Tweet', foreign_key: 'school_b_id'
144
+
145
+ end
146
+
147
+ ```
148
+
149
+
150
+
151
+ tweet.controller.rb
152
+
153
+
154
+
155
+ ```
156
+
157
+ class TweetsController < ApplicationController
158
+
159
+
160
+
161
+ before_action :search_tweet,only:[:show,:destroy,:edit,:update]
162
+
163
+ before_action :move_to_index,except: :index
164
+
165
+ before_action :set_category, only: [:index,:new, :edit, :create, :update, :destroy]
166
+
167
+
168
+
169
+ def index
170
+
171
+ @tweets = Tweet.includes(:user).page(params[:page]).per(5).order("created_at DESC")
172
+
173
+ end
174
+
175
+
176
+
177
+ def new
178
+
179
+ @tweet=Tweet.new
180
+
181
+ respond_to do |format|
182
+
183
+ format.html
184
+
185
+ format.json do
186
+
187
+ @category_children = Category.find(params[:tournament_id]).children
188
+
189
+ end
190
+
191
+ end
192
+
193
+ end
194
+
195
+
196
+
197
+ def create
198
+
199
+ @tweet=Tweet.create(tweet_params)
200
+
201
+ render "new" unless @tweet.save
202
+
203
+ end
204
+
205
+
206
+
207
+ def show
208
+
209
+ @nickname=current_user.nickname
210
+
211
+ @comments = @tweet.comments.includes(:user)
212
+
213
+ @like = Like.new
214
+
215
+ end
216
+
217
+
218
+
219
+ def destroy
220
+
221
+ @tweet.destroy if @tweet.user_id == current_user.id
222
+
223
+ end
224
+
225
+
226
+
227
+ def edit
228
+
229
+ @category_children = Category.find(params[:id]).children
230
+
231
+ end
232
+
233
+
234
+
235
+ def update
236
+
237
+ @tweet.update(tweet_params) if @tweet.user_id == current_user.id || current_user.admin
238
+
239
+ redirect_to action: :show
240
+
241
+ end
242
+
243
+
244
+
245
+ def get_category_children
246
+
247
+ @category_children = Category.find(params[:tournament_id]).children
248
+
249
+ end
250
+
251
+
252
+
253
+ def search_tweet
254
+
255
+ @tweet = Tweet.find(params[:id])
256
+
257
+ end
258
+
259
+
260
+
261
+
262
+
263
+ def move_to_index
264
+
265
+ redirect_to action: :index unless user_signed_in?
266
+
267
+ end
268
+
269
+
270
+
271
+ private
272
+
273
+ def set_category
274
+
275
+ @category_parent_array = Category.where(ancestry: nil)
276
+
277
+ end
278
+
279
+
280
+
281
+ def tweet_params
282
+
283
+ params.require(:tweet).permit(:image,:text,:title_info,:school_a_score,:school_b_score,:school_a_id,:school_b_id,:tournament_id).merge(user_id: current_user.id)
284
+
285
+ end
286
+
287
+ end
288
+
289
+ ```
290
+
291
+
292
+
293
+ edit.html.erb
294
+
295
+
296
+
297
+ ```
298
+
299
+ <div class="contents row">
300
+
301
+ <%= form_with(model: @tweet,local: true) do |f|%>
302
+
303
+ <%= f.label "大会選択" %>
304
+
305
+ <%=f.select :tournament_id, options_for_select( @category_parent_array.map{|c| [c[:name], c[:id]]},selected: @tweet.tournament_id),{include_blank:"大会を選択してください"}, { class: "parent_category_box", id: "parent_category"}%>
306
+
307
+ <div class="school"></div>
308
+
309
+ <%=f.select :school_a_id,options_for_select(@category_children.map{|c| [c[:name], c[:id]]},selected: @tweet.tournament_id),{},{class: "tournament_select_child",id: "children_category"} %>
310
+
311
+ <div class="school2"></div>
312
+
313
+ <%= render partial: "form",locals: {f: f} %>
314
+
315
+ <% end %>
316
+
317
+ <%= link_to "戻る","/tweets/#{@tweet.id}",class:"back"%>
318
+
319
+ </div>
320
+
321
+ ```
322
+
323
+
324
+
325
+ _form.html.erb
326
+
327
+
328
+
329
+ ```
330
+
331
+ <p>高校A 得点</p>
332
+
333
+ <%= f.select :school_a_score, options_for_select((1..50).to_a,selected: @tweet.school_a_score),{include_blank:"得点を選択してください"} %>
334
+
335
+ <p>高校B 得点</p>
336
+
337
+ <%= f.select :school_b_score, options_for_select((1..50).to_a,@tweet.school_b_score),{include_blank: "得点を選択してください"} %>
338
+
339
+ <%= f.text_field :title_info,placeholder: "タイトル",class: "game_record",value: "#{@tweet.title_info}" %>
340
+
341
+ <%= f.text_area :text, cols: 30 ,rows: 10,value: "#{@tweet.text}" %>
342
+
343
+ <%= f.label :image, class: "form-image" do %>
344
+
345
+ <h4>画像投稿</h4>
346
+
347
+ <i class="fa fa-image"></i>
348
+
349
+ <%= f.file_field :image, class: 'input-box_image_file',value: "#{@tweet.image}"%>
350
+
351
+ <% end %>
352
+
353
+ <%=f.submit value: "SENT",class: "game_record"%>
354
+
355
+ ```