質問編集履歴
1
処理内容を追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -9,3 +9,79 @@
|
|
9
9
|
|
10
10
|
|
11
11
|
何か良い方法はないのでしょうか
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
### 追記
|
16
|
+
|
17
|
+
処理内容は以下の通りになります
|
18
|
+
|
19
|
+
ビューから返るチェックボックスの値はチェック時に1、非チェック時に0です
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
コントローラー
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
|
29
|
+
# products_controller.rb
|
30
|
+
|
31
|
+
def create
|
32
|
+
|
33
|
+
first_user_id = current_user.id if params[:first_user_id] == 1
|
34
|
+
|
35
|
+
second_user_id = current_user.id if params[:second_user_id] == 1
|
36
|
+
|
37
|
+
@product = Product.create(product_params)
|
38
|
+
|
39
|
+
@product.update(build_user_id: current_user.id)
|
40
|
+
|
41
|
+
redirect_to product_path(@product.id)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def product_params
|
50
|
+
|
51
|
+
params.require(:product).permit(:title, :statement, :first_user_id, :second_user_id)
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
```
|
56
|
+
|
57
|
+
ビュー
|
58
|
+
|
59
|
+
```haml
|
60
|
+
|
61
|
+
.form
|
62
|
+
|
63
|
+
=form_with model: @project, local: true do |form|
|
64
|
+
|
65
|
+
.build-user{'data-current_user': current_user.id}
|
66
|
+
|
67
|
+
=current_user.name
|
68
|
+
|
69
|
+
=form.label :title, 'Title'
|
70
|
+
|
71
|
+
=form.text_field :title
|
72
|
+
|
73
|
+
=form.label :statement
|
74
|
+
|
75
|
+
=form.text_area :statement, 'Statement'
|
76
|
+
|
77
|
+
=form.label :first_user_id, 'First User'
|
78
|
+
|
79
|
+
=form.check_box :first_user_id
|
80
|
+
|
81
|
+
=form.label :second_user_id, 'Second User'
|
82
|
+
|
83
|
+
=form.check_box :second_user_id
|
84
|
+
|
85
|
+
=form.submit
|
86
|
+
|
87
|
+
```
|