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

回答編集履歴

5

誤字の修正

2019/04/17 05:03

投稿

ruby_math
ruby_math

スコア90

answer CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  上記を試してみてください。
24
24
 
25
- ===========================================================================
25
+ =======================================================================
26
26
 
27
27
  確かにHidden_fieldは危険ですね、以下を試してみてください。
28
28
 

4

誤字の修正

2019/04/17 05:03

投稿

ruby_math
ruby_math

スコア90

answer CHANGED
@@ -31,7 +31,7 @@
31
31
 
32
32
  respond_to do |format|
33
33
  if @company = Company.find(params[:id])
34
- @company.updated_user_id == current_user.id
34
+ @company.updated_user_id = current_user.id
35
35
  @company.update(company_params)
36
36
  format.html { redirect_to company_path, notice: 'Company was successfully updated.' }
37
37
  format.json { render :show, status: :ok, location: @company }

3

誤字を削除

2019/04/17 04:47

投稿

ruby_math
ruby_math

スコア90

answer CHANGED
@@ -29,8 +29,6 @@
29
29
  ```rb
30
30
  def update
31
31
 
32
- company_params[:updated_user_id] = current_user.id #deviseを利用
33
-
34
32
  respond_to do |format|
35
33
  if @company = Company.find(params[:id])
36
34
  @company.updated_user_id == current_user.id

2

回答を追記

2019/04/17 04:45

投稿

ruby_math
ruby_math

スコア90

answer CHANGED
@@ -20,4 +20,29 @@
20
20
 
21
21
  やりたいこととしては、現在ログインされているユーザーのIDをupdated_user_idに代入したいと言うことでよろしいでしょうか?
22
22
 
23
- 上記を試してみてください。
23
+ 上記を試してみてください。
24
+
25
+ ===========================================================================
26
+
27
+ 確かにHidden_fieldは危険ですね、以下を試してみてください。
28
+
29
+ ```rb
30
+ def update
31
+
32
+ company_params[:updated_user_id] = current_user.id #deviseを利用
33
+
34
+ respond_to do |format|
35
+ if @company = Company.find(params[:id])
36
+ @company.updated_user_id == current_user.id
37
+ @company.update(company_params)
38
+ format.html { redirect_to company_path, notice: 'Company was successfully updated.' }
39
+ format.json { render :show, status: :ok, location: @company }
40
+ else
41
+ format.html { render :edit }
42
+ format.json { render json: @company.errors, status: :unprocessable_entity }
43
+ end
44
+ end
45
+ end
46
+ ```
47
+
48
+ こちらでいかがでしょうか?

1

追記

2019/04/17 04:45

投稿

ruby_math
ruby_math

スコア90

answer CHANGED
@@ -12,7 +12,7 @@
12
12
  <%# @updated_user_id変数に現在ログインされているユーザーのIDを代入する %>
13
13
  <% @updated_user_id = current_user.id %>
14
14
 
15
- <%# 隠しフォーム %>
15
+ <%# 隠しフォーム @updated_user_idをvalueとして持たせてあげることで現在ログインされているユーザーのIDが送られる %>
16
16
  <%= f.hidden_field :updated_user_id, value:"#{@updated_user_id}" %>
17
17
 
18
18
  <% end %>