teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

処理内容を追記しました

2020/05/30 17:19

投稿

_dana
_dana

スコア15

title CHANGED
File without changes
body CHANGED
@@ -3,4 +3,42 @@
3
3
 
4
4
  createアクション内でif文を用いてparamsの値を調べてupdateで指定した値を保存するという方法を考えたのですが、どうやらアクション内ではparamsの値を直接取得することはできないみたいでエラーは出ませんが処理がスルーされてしまいます
5
5
 
6
- 何か良い方法はないのでしょうか
6
+ 何か良い方法はないのでしょうか
7
+
8
+ ### 追記
9
+ 処理内容は以下の通りになります
10
+ ビューから返るチェックボックスの値はチェック時に1、非チェック時に0です
11
+
12
+ コントローラー
13
+
14
+ ```ruby
15
+ # products_controller.rb
16
+ def create
17
+ first_user_id = current_user.id if params[:first_user_id] == 1
18
+ second_user_id = current_user.id if params[:second_user_id] == 1
19
+ @product = Product.create(product_params)
20
+ @product.update(build_user_id: current_user.id)
21
+ redirect_to product_path(@product.id)
22
+ end
23
+
24
+ private
25
+ def product_params
26
+ params.require(:product).permit(:title, :statement, :first_user_id, :second_user_id)
27
+ end
28
+ ```
29
+ ビュー
30
+ ```haml
31
+ .form
32
+ =form_with model: @project, local: true do |form|
33
+ .build-user{'data-current_user': current_user.id}
34
+ =current_user.name
35
+ =form.label :title, 'Title'
36
+ =form.text_field :title
37
+ =form.label :statement
38
+ =form.text_area :statement, 'Statement'
39
+ =form.label :first_user_id, 'First User'
40
+ =form.check_box :first_user_id
41
+ =form.label :second_user_id, 'Second User'
42
+ =form.check_box :second_user_id
43
+ =form.submit
44
+ ```