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

質問編集履歴

4

自己解決したので、問題個所以外を削除

2015/11/18 09:54

投稿

rails_beginner
rails_beginner

スコア11

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,6 @@
6
6
  t.string :name
7
7
  end
8
8
  ```
9
-
10
9
  子テーブル(Son):
11
10
  ```rails
12
11
  create_table :sons do |t|
@@ -14,7 +13,6 @@
14
13
  t.integer :age
15
14
  end
16
15
  ```
17
-
18
16
  このようなテーブル定義で、すでに親テーブルのnameには以下のようにデータが入っているとします。
19
17
  #<ActiveRecordRelation:: [#<Parent id: 1, name:"abc">, #<Parent id: 2, name:"def">]>
20
18
 
@@ -23,7 +21,6 @@
23
21
 
24
22
  # エラー内容
25
23
  Couldn't find Parent with ID=1 for Son with ID=
26
-
27
24
  ```rails
28
25
  def create
29
26
  @son = Son.new(son_params) ←エラー個所
@@ -31,7 +28,6 @@
31
28
  respond_to do |format|
32
29
  if @son.save
33
30
  ```
34
-
35
31
  Request
36
32
 
37
33
  Parameters:
@@ -41,30 +37,12 @@
41
37
  "parent_attributes"=>{"id"=>"1"}},
42
38
  "commit"=>"Create Son"}
43
39
 
44
-
45
40
  "parent_attributes"=>{"id"=>"1"}}, というリクエストが贈られているにもかかわらずIDが空白になっているので、Parentのレコードの検索の際にエラーが発生しているのだと思いますが、これ以上は此方で調べてもわかりませんでした。
46
41
  初投稿ですので至らぬ点等あると思います。情報が不足している場合はそのことを書き込んでいただけると助かります。
47
42
 
48
-
49
43
  以下はモデル、フォーム、コントローラーの抜粋です。
44
+ **# 追記 フォームに問題があることが分かったので、フォーム以外のコードは削除します。**
50
45
 
51
- # モデル
52
- parent.rb
53
- ```rails
54
- class Parent < ActiveRecord::Base
55
- has_many :son
56
- end
57
- ```
58
-
59
- son.rb
60
- ```rails
61
- class Son < ActiveRecord::Base
62
- belongs_to :parent
63
- accepts_nested_attributes_for :parent
64
- end
65
- ```
66
-
67
-
68
46
  #フォーム
69
47
  ```rails
70
48
  <%= form_for(@son) do |f| %>
@@ -81,27 +59,4 @@
81
59
  <% end %>
82
60
  <%= f.submit %>
83
61
  <% end %>
84
- ```
85
-
86
-
87
- # コントローラー
88
- ```rails
89
- def new
90
- @son = Son.new
91
- @son.build_parent
92
- end
93
-
94
- def update
95
- respond_to do |format|
96
- if @son.update(son_params)
97
- format.html { redirect_to @son, notice: 'Son was successfully updated.' }
98
- format.json { render :show, status: :ok, location: @son }
99
- else ...
100
- end
101
- end
102
- end
103
-
104
- def son_params
105
- params.require(:son).permit(:parent_id, :age, parent_attributes: [:id])
106
- end
107
62
  ```

3

自己解決したので、問題個所以外を削除

2015/11/18 09:54

投稿

rails_beginner
rails_beginner

スコア11

title CHANGED
File without changes
body CHANGED
File without changes

2

コードの部分をコードの書式に変換

2015/11/18 09:51

投稿

rails_beginner
rails_beginner

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,15 +1,19 @@
1
1
  子のフォームから親の名前をcollection_selectで探し、parent_idに代入しようと考えています。
2
2
 
3
3
  親テーブル(Parent):
4
+ ```rails
4
5
  create_table :parents do |t|
5
6
  t.string :name
6
7
  end
8
+ ```
7
9
 
8
10
  子テーブル(Son):
11
+ ```rails
9
12
  create_table :sons do |t|
10
13
  t.integer :parent_id
11
14
  t.integer :age
12
15
  end
16
+ ```
13
17
 
14
18
  このようなテーブル定義で、すでに親テーブルのnameには以下のようにデータが入っているとします。
15
19
  #<ActiveRecordRelation:: [#<Parent id: 1, name:"abc">, #<Parent id: 2, name:"def">]>
@@ -20,13 +24,13 @@
20
24
  # エラー内容
21
25
  Couldn't find Parent with ID=1 for Son with ID=
22
26
 
23
- ...
27
+ ```rails
24
- def create
28
+ def create
25
- @son = Son.new(son_params) ←エラー個所
29
+ @son = Son.new(son_params) ←エラー個所
26
30
 
27
- respond_to do |format|
31
+ respond_to do |format|
28
- if @son.save
32
+ if @son.save
29
- ...
33
+ ```
30
34
 
31
35
  Request
32
36
 
@@ -46,18 +50,23 @@
46
50
 
47
51
  # モデル
48
52
  parent.rb
53
+ ```rails
49
54
  class Parent < ActiveRecord::Base
50
55
  has_many :son
51
56
  end
57
+ ```
52
58
 
53
59
  son.rb
60
+ ```rails
54
61
  class Son < ActiveRecord::Base
55
62
  belongs_to :parent
56
63
  accepts_nested_attributes_for :parent
57
64
  end
65
+ ```
58
66
 
59
67
 
60
68
  #フォーム
69
+ ```rails
61
70
  <%= form_for(@son) do |f| %>
62
71
  ...
63
72
  <div class="field">
@@ -72,9 +81,11 @@
72
81
  <% end %>
73
82
  <%= f.submit %>
74
83
  <% end %>
84
+ ```
75
85
 
76
86
 
77
87
  # コントローラー
88
+ ```rails
78
89
  def new
79
90
  @son = Son.new
80
91
  @son.build_parent
@@ -92,4 +103,5 @@
92
103
 
93
104
  def son_params
94
105
  params.require(:son).permit(:parent_id, :age, parent_attributes: [:id])
95
- end
106
+ end
107
+ ```

1

タイトルの修正

2015/11/17 05:56

投稿

rails_beginner
rails_beginner

スコア11

title CHANGED
@@ -1,1 +1,1 @@
1
- Railsで子のフォームから親を参照するとき、RecordNotFoundエラーが発生する
1
+ Railsで子のフォームから親をcollection_selectで参照するとき、RecordNotFoundエラーが発生する
body CHANGED
@@ -1,3 +1,5 @@
1
+ 子のフォームから親の名前をcollection_selectで探し、parent_idに代入しようと考えています。
2
+
1
3
  親テーブル(Parent):
2
4
  create_table :parents do |t|
3
5
  t.string :name
@@ -12,7 +14,7 @@
12
14
  このようなテーブル定義で、すでに親テーブルのnameには以下のようにデータが入っているとします。
13
15
  #<ActiveRecordRelation:: [#<Parent id: 1, name:"abc">, #<Parent id: 2, name:"def">]>
14
16
 
15
- の時、子のフォームから親の名前collection_selectで探し、parent_idに代入しようと考えているのでこの時にRecordNotFoundエラーが発生してしまいます。
17
+ Submitると、RecordNotFoundエラーが発生してしまいます。
16
18
 
17
19
 
18
20
  # エラー内容