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

質問編集履歴

10

書式の改善

2020/05/22 05:25

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -97,11 +97,10 @@
97
97
  <div><%= f.submit "レビューする" %></div>
98
98
  <% end %>
99
99
  <div><%= link_to "トップページ", products_path %></div>
100
+ ```
101
+ ```
102
+ エラーコード
100
103
 
101
- ```
102
-
103
- エラーコード
104
- ```
105
104
  Started POST "/reviews" for ::1 at 2020-05-21 11:01:42 +0900
106
105
  Processing by ReviewsController#create as JS
107
106
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiH793Sir412NVnz6Gs9R6/AIPH0nwLZG/MHrTuntsZn8xvcwEAMbqY9jLNAPo6sSeDfPk8AidgHahkG9DrFtg==", "review"=>{"total"=>"1", "package"=>"3", "price"=>"2", "color"=>"2", "amount"=>"1", "naming"=>"5", "concept"=>"5", "balance"=>"5", "comment"=>"aaaaaaaaa"}, "commit"=>"レビューする"}
@@ -117,8 +116,10 @@
117
116
  Rendered reviews/new.html.erb within layouts/application (17.2ms)
118
117
  Completed 200 OK in 82ms (Views: 69.7ms | ActiveRecord: 1.4ms)
119
118
  ```
119
+ ```
120
120
  ログ
121
- ```
121
+
122
+
122
123
  ActionView::Template::Error (undefined method `id' for nil:NilClass):
123
124
  54: <div><%= f.submit "レビューする" %></div>
124
125
  55: <% end %>
@@ -129,7 +130,7 @@
129
130
  app/controllers/reviews_controller.rb:20:in `create'
130
131
  ```
131
132
  review.save!にした時のログ
132
- ```
133
+
133
134
  app/views/reviews/new.html.erb:57:in `_app_views_reviews_new_html_erb___2040207276415451298_70112480660040'
134
135
  app/controllers/reviews_controller.rb:20:in `create'
135
136
  Started POST "/reviews" for ::1 at 2020-05-21 22:08:48 +0900
@@ -147,19 +148,20 @@
147
148
  ↳ app/controllers/reviews_controller.rb:17
148
149
  Completed 422 Unprocessable Entity in 54ms (ActiveRecord: 6.8ms)
149
150
 
150
-
151
-
152
151
  ActiveRecord::RecordInvalid (Validation failed: Product must exist, Product can't be blank):
153
152
 
154
153
  app/controllers/reviews_controller.rb:17:in `create'
154
+ ```
155
+ ```
155
156
 
156
- ```
157
157
  application_controller
158
- ```
158
+
159
+
159
160
  class ApplicationController < ActionController::Base
160
161
  before_action :set_current_user
161
162
  def set_current_user
162
163
  @current_user = User.find_by(id: session[:user_id])
163
164
  end
164
165
  end
166
+
165
167
  ```

9

formの中に入れてみました。

2020/05/22 05:25

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -42,7 +42,7 @@
42
42
  ```
43
43
  <h1>Review sheets</h1>
44
44
  <%= flash[:notice] %>
45
- <%= form_with model: @review do |f| %>
45
+ <%= form_with model: @product,@review do |f| %>
46
46
  <div><%= f.label :'総合' %><div>
47
47
  <div><%= f.radio_button :total, "1" %> 1<div>
48
48
  <div><%= f.radio_button :total, "2" %> 2<div>
@@ -93,12 +93,13 @@
93
93
  <div><%= f.radio_button :balance, "5" %> 5<div>
94
94
  <div><%= f.label :'コメント' %></div>
95
95
  <div><%= f.text_field :comment %></div>
96
+ <div><%= f.hidden_field_tag :product_id, @product.id %></div>
96
97
  <div><%= f.submit "レビューする" %></div>
97
98
  <% end %>
98
99
  <div><%= link_to "トップページ", products_path %></div>
99
- <%= hidden_field_tag :product_id, @product.id %>
100
- ```
101
100
 
101
+ ```
102
+
102
103
  エラーコード
103
104
  ```
104
105
  Started POST "/reviews" for ::1 at 2020-05-21 11:01:42 +0900

8

application_controllerを追記

2020/05/22 03:55

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -152,4 +152,13 @@
152
152
 
153
153
  app/controllers/reviews_controller.rb:17:in `create'
154
154
 
155
+ ```
156
+ application_controller
157
+ ```
158
+ class ApplicationController < ActionController::Base
159
+ before_action :set_current_user
160
+ def set_current_user
161
+ @current_user = User.find_by(id: session[:user_id])
162
+ end
163
+ end
155
164
  ```

7

review.save!にした時のログ

2020/05/21 13:22

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -126,4 +126,30 @@
126
126
 
127
127
  app/views/reviews/new.html.erb:57:in `_app_views_reviews_new_html_erb___2040207276415451298_70112480660040'
128
128
  app/controllers/reviews_controller.rb:20:in `create'
129
+ ```
130
+ review.save!にした時のログ
131
+ ```
132
+ app/views/reviews/new.html.erb:57:in `_app_views_reviews_new_html_erb___2040207276415451298_70112480660040'
133
+ app/controllers/reviews_controller.rb:20:in `create'
134
+ Started POST "/reviews" for ::1 at 2020-05-21 22:08:48 +0900
135
+ (0.2ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
136
+ ↳ /Users/mami.igarashi/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.2/lib/active_record/log_subscriber.rb:98
137
+ Processing by ReviewsController#create as JS
138
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiH793Sir412NVnz6Gs9R6/AIPH0nwLZG/MHrTuntsZn8xvcwEAMbqY9jLNAPo6sSeDfPk8AidgHahkG9DrFtg==", "review"=>{"total"=>"5", "package"=>"3", "price"=>"2", "color"=>"2", "amount"=>"1", "naming"=>"5", "concept"=>"5", "balance"=>"5", "comment"=>"aaaaaaaaa"}, "commit"=>"レビューする"}
139
+ User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
140
+ ↳ app/controllers/application_controller.rb:4
141
+ (0.2ms) BEGIN
142
+ ↳ app/controllers/reviews_controller.rb:17
143
+ CACHE User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
144
+ ↳ app/controllers/reviews_controller.rb:17
145
+ (0.2ms) ROLLBACK
146
+ ↳ app/controllers/reviews_controller.rb:17
147
+ Completed 422 Unprocessable Entity in 54ms (ActiveRecord: 6.8ms)
148
+
149
+
150
+
151
+ ActiveRecord::RecordInvalid (Validation failed: Product must exist, Product can't be blank):
152
+
153
+ app/controllers/reviews_controller.rb:17:in `create'
154
+
129
155
  ```

6

controllerのnew を追加newのviewファイルに<%= hidden_field_tag :product_id, @product.id %>を追加。ログを追加。

2020/05/21 13:12

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -18,7 +18,12 @@
18
18
 
19
19
  reviews_controller
20
20
  ```
21
+ def new
22
+ @product = Product.find(params[:id])
23
+ @review = Review.new
24
+ end
25
+
21
- def create
26
+  def create
22
27
  review = Review.new(review_params)
23
28
  if review.save
24
29
  redirect_to products_path, notice: "保存が成功しました"
@@ -91,7 +96,7 @@
91
96
  <div><%= f.submit "レビューする" %></div>
92
97
  <% end %>
93
98
  <div><%= link_to "トップページ", products_path %></div>
94
-
99
+ <%= hidden_field_tag :product_id, @product.id %>
95
100
  ```
96
101
 
97
102
  エラーコード
@@ -110,4 +115,15 @@
110
115
  Rendering reviews/new.html.erb within layouts/application
111
116
  Rendered reviews/new.html.erb within layouts/application (17.2ms)
112
117
  Completed 200 OK in 82ms (Views: 69.7ms | ActiveRecord: 1.4ms)
118
+ ```
119
+ ログ
120
+ ```
121
+ ActionView::Template::Error (undefined method `id' for nil:NilClass):
122
+ 54: <div><%= f.submit "レビューする" %></div>
123
+ 55: <% end %>
124
+ 56: <div><%= link_to "トップページ", products_path %></div>
125
+ 57: <%= hidden_field_tag :product_id, @product.id %>
126
+
127
+ app/views/reviews/new.html.erb:57:in `_app_views_reviews_new_html_erb___2040207276415451298_70112480660040'
128
+ app/controllers/reviews_controller.rb:20:in `create'
113
129
  ```

5

コード追加

2020/05/21 12:45

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -19,9 +19,7 @@
19
19
  reviews_controller
20
20
  ```
21
21
  def create
22
- review = Review.new(review_params)
22
+ review = Review.new(review_params
23
- @product = Product.find(params[:id])
24
- @reviews = @Product.reviews
25
23
  if review.save
26
24
  redirect_to products_path, notice: "保存が成功しました"
27
25
  else

4

コード追加

2020/05/21 02:06

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -31,7 +31,7 @@
31
31
 
32
32
  private
33
33
  def review_params
34
- params.require(:review).permit(:total, :package, :price, :color, :amount, :naming, :concept, :balance, :comment)
34
+ params.require(:review).permit(:total, :package, :price, :color, :amount, :naming, :concept, :balance, :comment).merge(user_id: @current_user.id, product_id: params[:product_id])
35
35
  end
36
36
  ```
37
37
 
@@ -94,4 +94,22 @@
94
94
  <% end %>
95
95
  <div><%= link_to "トップページ", products_path %></div>
96
96
 
97
+ ```
98
+
99
+ エラーコード
100
+ ```
101
+ Started POST "/reviews" for ::1 at 2020-05-21 11:01:42 +0900
102
+ Processing by ReviewsController#create as JS
103
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"HiH793Sir412NVnz6Gs9R6/AIPH0nwLZG/MHrTuntsZn8xvcwEAMbqY9jLNAPo6sSeDfPk8AidgHahkG9DrFtg==", "review"=>{"total"=>"1", "package"=>"3", "price"=>"2", "color"=>"2", "amount"=>"1", "naming"=>"5", "concept"=>"5", "balance"=>"5", "comment"=>"aaaaaaaaa"}, "commit"=>"レビューする"}
104
+ User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
105
+ ↳ app/controllers/application_controller.rb:4
106
+ (0.4ms) BEGIN
107
+ ↳ app/controllers/reviews_controller.rb:16
108
+ CACHE User Load (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
109
+ ↳ app/controllers/reviews_controller.rb:16
110
+ (0.5ms) ROLLBACK
111
+ ↳ app/controllers/reviews_controller.rb:16
112
+ Rendering reviews/new.html.erb within layouts/application
113
+ Rendered reviews/new.html.erb within layouts/application (17.2ms)
114
+ Completed 200 OK in 82ms (Views: 69.7ms | ActiveRecord: 1.4ms)
97
115
  ```

3

書式の改善

2020/05/21 02:05

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- オリジナルアプリで商品のレビューページを作成中ですが、アソシエーションで関連づけたusers.idとproduct.idをnewページからcreateした時に、うまくDBに格納できません。privateメソッドにmargeで追加してみたりしましたがうまく格納でき。ご教授頂けたらと思います。
1
+ オリジナルアプリで商品のレビューページを作成中ですが、アソシエーションで関連づけたusers.idとproduct.idをnewページからcreateした時に、うまくDBに格納できません。privateメソッドにmargeで追加してみたりしましたが引数の渡し方に問題があるのか、うまく格納できません。ご教授頂けたらと思います。
2
2
 
3
3
  user model
4
4
  ```

2

誤字

2020/05/21 01:26

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- オリジナルアプリで商品のレビューページを作成中ですが、アソシエーションで関連づけたusers.idとproduct.idをcreateした時に、うまくDBに格納できません。privateメソッドにmargeで追加してみたりしましたがうまく格納できず。ご教授頂けたらと思います。
1
+ オリジナルアプリで商品のレビューページを作成中ですが、アソシエーションで関連づけたusers.idとproduct.idをnewページからcreateした時に、うまくDBに格納できません。privateメソッドにmargeで追加してみたりしましたがうまく格納できず。ご教授頂けたらと思います。
2
2
 
3
3
  user model
4
4
  ```

1

new.html.erbを追加しました。

2020/05/21 01:22

投稿

mamikko
mamikko

スコア7

title CHANGED
File without changes
body CHANGED
@@ -33,4 +33,65 @@
33
33
  def review_params
34
34
  params.require(:review).permit(:total, :package, :price, :color, :amount, :naming, :concept, :balance, :comment)
35
35
  end
36
+ ```
37
+
38
+ new.html.erb
39
+ ```
40
+ <h1>Review sheets</h1>
41
+ <%= flash[:notice] %>
42
+ <%= form_with model: @review do |f| %>
43
+ <div><%= f.label :'総合' %><div>
44
+ <div><%= f.radio_button :total, "1" %> 1<div>
45
+ <div><%= f.radio_button :total, "2" %> 2<div>
46
+ <div><%= f.radio_button :total, "3" %> 3<div>
47
+ <div><%= f.radio_button :total, "4" %> 4<div>
48
+ <div><%= f.radio_button :total, "5" %> 5<div>
49
+ <div><%= f.label :'パッケージ' %><div>
50
+ <div><%= f.radio_button :package, "1" %> 1<div>
51
+ <div><%= f.radio_button :package, "2" %> 2<div>
52
+ <div><%= f.radio_button :package, "3" %> 3<div>
53
+ <div><%= f.radio_button :package, "4" %> 4<div>
54
+ <div><%= f.radio_button :package, "5" %> 5<div>
55
+ <div><%= f.label :'価格' %><div>
56
+ <div><%= f.radio_button :price, "1" %> 1<div>
57
+ <div><%= f.radio_button :price, "2" %> 2<div>
58
+ <div><%= f.radio_button :price, "3" %> 3<div>
59
+ <div><%= f.radio_button :price, "4" %> 4<div>
60
+ <div><%= f.radio_button :price, "5" %> 5<div>
61
+ <div><%= f.label :'カラー' %><div>
62
+ <div><%= f.radio_button :color, "1" %> 1<div>
63
+ <div><%= f.radio_button :color, "2" %> 2<div>
64
+ <div><%= f.radio_button :color, "3" %> 3<div>
65
+ <div><%= f.radio_button :color, "4" %> 4<div>
66
+ <div><%= f.radio_button :color, "5" %> 5<div>
67
+ <div><%= f.label :'量・大きさ' %><div>
68
+ <div><%= f.radio_button :amount, "1" %> 1<div>
69
+ <div><%= f.radio_button :amount, "2" %> 2<div>
70
+ <div><%= f.radio_button :amount, "3" %> 3<div>
71
+ <div><%= f.radio_button :amount, "4" %> 4<div>
72
+ <div><%= f.radio_button :amount, "5" %> 5<div>
73
+ <div><%= f.label :'ネーミング' %><div>
74
+ <div><%= f.radio_button :naming, "1" %> 1<div>
75
+ <div><%= f.radio_button :naming, "2" %> 2<div>
76
+ <div><%= f.radio_button :naming, "3" %> 3<div>
77
+ <div><%= f.radio_button :naming, "4" %> 4<div>
78
+ <div><%= f.radio_button :naming, "5" %> 5<div>
79
+ <div><%= f.label :'コンセプト' %><div>
80
+ <div><%= f.radio_button :concept, "1" %> 1<div>
81
+ <div><%= f.radio_button :concept, "2" %> 2<div>
82
+ <div><%= f.radio_button :concept, "3" %> 3<div>
83
+ <div><%= f.radio_button :concept, "4" %> 4<div>
84
+ <div><%= f.radio_button :concept, "5" %> 5<div>
85
+ <div><%= f.label :'バランス' %><div>
86
+ <div><%= f.radio_button :balance, "1" %> 1<div>
87
+ <div><%= f.radio_button :balance, "2" %> 2<div>
88
+ <div><%= f.radio_button :balance, "3" %> 3<div>
89
+ <div><%= f.radio_button :balance, "4" %> 4<div>
90
+ <div><%= f.radio_button :balance, "5" %> 5<div>
91
+ <div><%= f.label :'コメント' %></div>
92
+ <div><%= f.text_field :comment %></div>
93
+ <div><%= f.submit "レビューする" %></div>
94
+ <% end %>
95
+ <div><%= link_to "トップページ", products_path %></div>
96
+
36
97
  ```