質問編集履歴

2

グラフのコード部分を指すコメントを追加いたしました。

2021/06/12 07:53

投稿

sh11821783
sh11821783

スコア0

test CHANGED
File without changes
test CHANGED
@@ -334,8 +334,6 @@
334
334
 
335
335
  <tr>
336
336
 
337
- ↓グラフのコードです
338
-
339
337
  <td>
340
338
 
341
339
  <%= l(Date.today, format: :long) %>
@@ -374,7 +372,7 @@
374
372
 
375
373
  <% end %>
376
374
 
377
-
375
+     ↓グラフのコードです
378
376
 
379
377
  <%= pie_chart @household_account_book %>
380
378
 

1

コントローラ及びshowページのコードを載せなおしました

2021/06/12 07:53

投稿

sh11821783
sh11821783

スコア0

test CHANGED
File without changes
test CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- ここに質問の内容を詳しく書いてください。
6
-
7
- (例)ruby(ruby on rails)で家計簿システムを作っています。
5
+ ruby(ruby on rails)で家計簿システムを作っています。
8
6
 
9
7
  各ページ毎(固定費ページ、変動費ページ、収入ページ)の各月毎に各項目(電気代や水道代)のグラフを実装中なのですが、それぞれ固定費の項目、変動費の項目、収入の「項目のデータを絞って、グラフにすることができません。
10
8
 
@@ -22,7 +20,29 @@
22
20
 
23
21
  ```
24
22
 
23
+ class UsersController < ApplicationController
24
+
25
+ before_action :set_user, only: [:show, :show_1, :show_2, :edit, :update, :destroy, :edit_basic_info, :update_basic_info]
26
+
27
+ before_action :logged_in_user, only: [:index, :edit, :update, :destroy, :edit_basic_info, :update_basic_info]
28
+
29
+ before_action :correct_user, only: [:edit, :update]
30
+
31
+ before_action :admin_user, only: [:destroy, :edit_basic_info, :update_basic_info]
32
+
33
+ before_action :set_one_month, only: [:show, :show_1, :show_2]
34
+
35
+
36
+
37
+ def index
38
+
39
+ @users = User.paginate(page: params[:page]).search(params[:search])
40
+
41
+ end
42
+
43
+ # 固定費画面
44
+
25
- def show
45
+ def show
26
46
 
27
47
  if params["select_year(1i)"].present? && params["select_month(2i)"].present? #&& params["select_month(3i)"].present?
28
48
 
@@ -52,13 +72,399 @@
52
72
 
53
73
  end
54
74
 
75
+ # 変動費画面
76
+
77
+ def show_1
78
+
79
+ if params["select_year(1i)"].present? && params["select_month(2i)"].present? #&& params["select_month(3i)"].present?
80
+
81
+ # パラメーターで2020(1i)-06(2i)-01(3i)というスタイルで表示されていたので、コードもそれに合わせてやる。
82
+
83
+ select_day = params["select_year(1i)"] + "-" +
84
+
85
+ # パラメーター上では、月日は「6」や「1」というふうに出ていたので上記のスタイルに合わせて比較させるので以下のよいうにフォーマットを合わせている。
86
+
87
+ format("%02d", params["select_month(2i)"]) + "-" +
88
+
89
+ format("%02d", params["select_month(3i)"])
90
+
91
+ # @first_day = select_day.to_date.beginning_of_month
92
+
93
+ # @last_day = @first_day.end_of_month
94
+
95
+ # @household_account_books = @user.household_account_books.where(worked_on: @first_day..@last_day).order(:worked_on)
96
+
97
+ params[:date] = select_day.to_date.beginning_of_month
98
+
99
+ set_one_month
100
+
101
+ end
102
+
103
+ @household_account_book = @user.household_account_books.find_by(worked_on: @first_day)
104
+
105
+ end
106
+
107
+ # 収入画面
108
+
109
+ def show_2
110
+
111
+ if params["select_year(1i)"].present? && params["select_month(2i)"].present? #&& params["select_month(3i)"].present?
112
+
113
+ # パラメーターで2020(1i)-06(2i)-01(3i)というスタイルで表示されていたので、コードもそれに合わせてやる。
114
+
115
+ select_day = params["select_year(1i)"] + "-" +
116
+
117
+ # パラメーター上では、月日は「6」や「1」というふうに出ていたので上記のスタイルに合わせて比較させるので以下のよいうにフォーマットを合わせている。
118
+
119
+ format("%02d", params["select_month(2i)"]) + "-" +
120
+
121
+ format("%02d", params["select_month(3i)"])
122
+
123
+ # @first_day = select_day.to_date.beginning_of_month
124
+
125
+ # @last_day = @first_day.end_of_month
126
+
127
+ # @household_account_books = @user.household_account_books.where(worked_on: @first_day..@last_day).order(:worked_on)
128
+
129
+ params[:date] = select_day.to_date.beginning_of_month
130
+
131
+ set_one_month
132
+
133
+ end
134
+
135
+ @household_account_book = @user.household_account_books.find_by(worked_on: @first_day)
136
+
137
+ end
138
+
139
+
140
+
141
+ def new
142
+
143
+ @user = User.new
144
+
145
+ end
146
+
147
+
148
+
149
+ def create
150
+
151
+ @user = User.new(user_params)
152
+
153
+ if @user.save
154
+
155
+ log_in @user
156
+
157
+ flash[:success] = '新規作成に成功しました。'
158
+
159
+ redirect_to @user
160
+
161
+ else
162
+
163
+ render :new
164
+
165
+ end
166
+
167
+ end
168
+
169
+
170
+
171
+ def edit
172
+
173
+ end
174
+
175
+
176
+
177
+ def update
178
+
179
+ if @user.update_attributes(user_params)
180
+
181
+ flash[:success] = "ユーザー情報を更新しました。"
182
+
183
+ redirect_to @user
184
+
185
+ else
186
+
187
+ render :edit
188
+
189
+ end
190
+
191
+ end
192
+
193
+
194
+
195
+ def destroy
196
+
197
+ @user.destroy
198
+
199
+ flash[:success] = "#{@user.name}のデータを削除しました。"
200
+
201
+ redirect_to users_url
202
+
203
+ end
204
+
205
+
206
+
207
+ def edit_basic_info
208
+
209
+ end
210
+
211
+
212
+
213
+ def update_basic_info
214
+
215
+ if @user.update_attributes(basic_info_params)
216
+
217
+ flash[:success] = "#{@user.name}の基本情報を更新しました。"
218
+
219
+ else
220
+
221
+ flash[:danger] = "#{@user.name}の更新は失敗しました。<br>" + @user.errors.full_messages.join("<br>")
222
+
223
+ end
224
+
225
+ redirect_to users_url
226
+
227
+ end
228
+
229
+
230
+
231
+ private
232
+
233
+
234
+
235
+ def user_params
236
+
237
+ params.require(:user).permit(:name, :email, :phone_number, :password, :password_confirmation)
238
+
239
+ end
240
+
241
+
242
+
243
+ def basic_info_params
244
+
245
+ params.require(:user).permit(:email, :phone_number, :password, :password_confirmation)
246
+
247
+ end
248
+
249
+
250
+
251
+ # beforフィルター
252
+
253
+
254
+
255
+ # paramsハッシュからユーザーを取得します。
256
+
257
+ def set_user
258
+
259
+ @user = User.find(params[:id])
260
+
261
+ end
262
+
263
+
264
+
265
+ # ログイン済みのユーザーか確認します。
266
+
267
+ def logged_in_user
268
+
269
+ unless logged_in?
270
+
271
+ store_location
272
+
273
+ flash[:danger] = "ログインしてください。"
274
+
275
+ redirect_to login_url
276
+
277
+ end
278
+
279
+ end
280
+
281
+
282
+
283
+ # アクセスしたユーザーが現在ログインしているユーザーか確認します。
284
+
285
+ def correct_user
286
+
287
+ redirect_to(root_url) unless current_user?(@user)
288
+
289
+ end
290
+
291
+
292
+
293
+ # システム管理権限所有かどうか判定します。
294
+
295
+ def admin_user
296
+
297
+ redirect_to root_url unless current_user.admin?
298
+
299
+ end
300
+
301
+ end
302
+
55
303
  ```
56
304
 
57
305
 
58
306
 
59
307
  ### 該当のソースコード(html)
60
308
 
309
+ ```<% provide(:title, @user.name) %>
310
+
311
+ <div>
312
+
313
+ <table class="table table-bordered table-condensed user-table">
314
+
315
+ <tr>
316
+
317
+ <td>名前:<%= @user.name %></td>
318
+
319
+ </tr>
320
+
321
+ </table>
322
+
323
+ </div>
324
+
325
+
326
+
327
+ <h1>【固定費一覧表】</h1>
328
+
329
+
330
+
331
+ <div>
332
+
333
+ <table class="table table-bordered table-condensed" id="table-attendances">
334
+
335
+ <tr>
336
+
337
+ ↓グラフのコードです
338
+
339
+ <td>
340
+
341
+ <%= l(Date.today, format: :long) %>
342
+
343
+ </td>
344
+
345
+ </tr>
346
+
347
+ <table>
348
+
349
+ </div>
350
+
351
+
352
+
353
+ <div>
354
+
355
+ <% @household_account_books.each do |h| %>
356
+
357
+ <% @all_total_income = @all_total_income.to_i + h.income_n.to_i + h.income_n_1.to_i + h.income_n_2.to_i + h.income_n_3.to_i + h.income_n_4.to_i + h.income_n_5.to_i
358
+
359
+ + h.income_n_6.to_i + h.income_n_7.to_i + h.income_n_8.to_i + h.income_n_9.to_i + h.income_n_10.to_i %>
360
+
361
+
362
+
363
+ <% @all_total_cost = @all_total_cost.to_i + h.fixed_n.to_i + h.fixed_n_1.to_i + h.fixed_n_2.to_i + h.fixed_n_3.to_i + h.fixed_n_4.to_i + h.fixed_n_5.to_i +
364
+
365
+ h.fixed_n_6.to_i + h.fixed_n_7.to_i + h.fixed_n_8.to_i + h.fixed_n_9.to_i + h.fixed_n_10.to_i + h.fixed_n_11.to_i +
366
+
367
+ h.fixed_n_12.to_i + h.fixed_n_13.to_i + h.fixed_n_14.to_i + h.fixed_n_15.to_i %>
368
+
369
+
370
+
371
+ <% @all_total_cost_1 = @all_total_cost_1.to_i + h.variable_n.to_i + h.variable_n_1.to_i + h.variable_n_2.to_i + h.variable_n_3.to_i + h.variable_n_4.to_i + h.variable_n_5.to_i
372
+
373
+ + h.variable_n_6.to_i + h.variable_n_7.to_i + h.variable_n_8.to_i + h.variable_n_9.to_i + h.income_n_10.to_i %>
374
+
375
+ <% end %>
376
+
377
+
378
+
61
- ```<%= pie_chart @household_account_book %>
379
+ <%= pie_chart @household_account_book %>
380
+
381
+
382
+
383
+ <table class="table table-bordered table-condensed" id="table-attendances">
384
+
385
+ <tr>
386
+
387
+ <th>収入</th>
388
+
389
+ <th>支出(固定費 + 変動費)</th>
390
+
391
+ <th>収支合計</th>
392
+
393
+ </tr>
394
+
395
+ <tr>
396
+
397
+ <td><%= "#{@all_total_income.to_s(:delimited)}円" %></td>
398
+
399
+ <td><%= "#{(@all_total_cost + @all_total_cost_1).to_s(:delimited)}円" "(#{@all_total_cost.to_s(:delimited)}円 + #{@all_total_cost_1.to_s(:delimited)}円)" %></td>
400
+
401
+ <td><%= "#{(@all_total_income.to_i - (@all_total_cost + @all_total_cost_1).to_i).to_s(:delimited)}円" %></td>
402
+
403
+ </tr>
404
+
405
+ <table>
406
+
407
+ </div>
408
+
409
+
410
+
411
+ <div class="center">
412
+
413
+ <%= form_with(url: user_path(@user), method: :get, local: true) do |f| %>
414
+
415
+ <%= f.date_select :select_year, {:discard_day => true, :discard_month => true, :use_month_numbers => true}, class: "form-control bootstrap-date" %>
416
+
417
+ <%= f.date_select :select_month, {:discard_day => true, :discard_year => true, :use_month_numbers => true}, class: "form-control bootstrap-date" %>
418
+
419
+ <button class="btn btn-primary"> 検索 </button><br><br>
420
+
421
+ <% end %>
422
+
423
+ </div>
424
+
425
+
426
+
427
+ <div class="center">
428
+
429
+ <%= link_to "⇦ 前月へ", user_path(date: @first_day.prev_month), class: "btn btn-info" %>
430
+
431
+ <%= link_to "1ヶ月の家計簿編集へ", household_account_books_edit_one_month_user_path(date: @first_day), class: "btn btn-success" %>
432
+
433
+ <%= link_to "次月へ ⇨", user_path(date: @first_day.next_month), class: "btn btn-info" %>
434
+
435
+ </div>
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+ <tr>
450
+
451
+ <td><%= "支出合計(固定)" %></td>
452
+
453
+ <% @all_total_cost = @total_cost.to_i + @total_cost_1.to_i + @total_cost_2.to_i + @total_cost_3.to_i + @total_cost_4.to_i + @total_cost_5.to_i +
454
+
455
+ @total_cost_6.to_i + @total_cost_7.to_i + @total_cost_8.to_i + @total_cost_9.to_i + @total_cost_10.to_i + @total_cost_11.to_i +
456
+
457
+ @total_cost_12.to_i + @total_cost_13.to_i + @total_cost_14.to_i + @total_cost_15.to_i %>
458
+
459
+ <td><%= "#{@all_total_cost.to_s(:delimited)}円" %></td>
460
+
461
+ </tr>
462
+
463
+ </tfoot>
464
+
465
+ </table>
466
+
467
+ </div>
62
468
 
63
469
  ```
64
470