回答編集履歴
3
追記 Rails way
test
CHANGED
@@ -12,36 +12,90 @@
|
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
**Rails wayに修正**
|
16
16
|
|
17
|
-
が、浅学のため`update`がそれで正しいかはちょっとやってみないとわかりません。
|
18
17
|
|
18
|
+
|
19
|
-
|
19
|
+
Rails wayな方法としては`accepts_nested_attributes_for`を用いて更新することになります。
|
20
|
+
|
21
|
+
|
20
22
|
|
21
23
|
|
22
24
|
|
23
25
|
```ruby
|
24
26
|
|
25
|
-
|
27
|
+
class User < ApplicationRecord
|
26
28
|
|
27
|
-
|
29
|
+
has_many :its, dependent: :destroy
|
28
30
|
|
31
|
+
accepts_nested_attributes_for :its
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
```erb
|
40
|
+
|
41
|
+
<%= form_with model: current_user, url: it_path, method: :patch, local: true do |form| %>
|
42
|
+
|
43
|
+
<%= form.fields_for :its do |field| %>
|
44
|
+
|
45
|
+
<%= field.label :situation %>
|
46
|
+
|
29
|
-
|
47
|
+
<%= field.text_field :situation, class: 'form-field', required: true %>
|
48
|
+
|
49
|
+
<br>
|
50
|
+
|
51
|
+
<%= field.label :behavior %>
|
52
|
+
|
53
|
+
<%= field.text_field :behavior, class: 'form-field', required: true %>
|
54
|
+
|
55
|
+
<% end %>
|
56
|
+
|
57
|
+
<%= form.submit '一括編集'%>
|
58
|
+
|
59
|
+
<%= link_to '戻る', it_path(current_user) %>
|
60
|
+
|
61
|
+
<% end %>
|
62
|
+
|
63
|
+
```
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
|
69
|
+
def edit
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
def update
|
76
|
+
|
77
|
+
if current_user.update(user_its_params)
|
78
|
+
|
79
|
+
flash[:success] = "編集しました!"
|
80
|
+
|
81
|
+
redirect_to it_path(current_user)
|
82
|
+
|
83
|
+
else
|
84
|
+
|
85
|
+
render 'its/edit'
|
30
86
|
|
31
87
|
end
|
32
88
|
|
33
|
-
|
89
|
+
end
|
34
90
|
|
35
|
-
params.require(:its).transform_values{|v| v.permit(:situation, :behavior)}
|
36
91
|
|
37
|
-
end
|
38
92
|
|
39
|
-
|
93
|
+
private
|
40
94
|
|
41
|
-
|
95
|
+
def user_its_params
|
42
96
|
|
43
|
-
|
97
|
+
params.require(:user).permit(its_attributes: [:situation, :behavior, :id])
|
44
98
|
|
45
|
-
|
99
|
+
end
|
46
100
|
|
47
101
|
```
|
2
修正
test
CHANGED
@@ -26,13 +26,13 @@
|
|
26
26
|
|
27
27
|
def its_params
|
28
28
|
|
29
|
-
params.slice(:its).permit(its: [:situation, :behavior])
|
29
|
+
params.slice(:its).permit(its: [:situation, :behavior]).require(:its)
|
30
30
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def its_params
|
34
34
|
|
35
|
-
params.require(:its).
|
35
|
+
params.require(:its).transform_values{|v| v.permit(:situation, :behavior)}
|
36
36
|
|
37
37
|
end
|
38
38
|
|
1
修正
test
CHANGED
@@ -16,13 +16,31 @@
|
|
16
16
|
|
17
17
|
が、浅学のため`update`がそれで正しいかはちょっとやってみないとわかりません。
|
18
18
|
|
19
|
+
**修正**
|
20
|
+
|
19
21
|
|
20
22
|
|
21
23
|
```ruby
|
22
24
|
|
25
|
+
# 以下のどれか
|
26
|
+
|
23
27
|
def its_params
|
24
28
|
|
25
|
-
params.permit(its: [:situation, :behavior])
|
29
|
+
params.slice(:its).permit(its: [:situation, :behavior])
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def its_params
|
34
|
+
|
35
|
+
params.require(:its).each_value{|v| v.permit(:situation, :behavior) }
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def its_params
|
40
|
+
|
41
|
+
# 出来ればコレは使いたくないです。
|
42
|
+
|
43
|
+
params.require(:its).permit!
|
26
44
|
|
27
45
|
end
|
28
46
|
|