質問編集履歴
4
モデル定義部分の追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -126,4 +126,30 @@
|
|
126
126
|
|
127
127
|
<% end %>
|
128
128
|
</div>
|
129
|
+
```
|
130
|
+
|
131
|
+
## 追記
|
132
|
+
target.rb
|
133
|
+
```ruby
|
134
|
+
class Target < ApplicationRecord
|
135
|
+
belongs_to :category
|
136
|
+
belongs_to :priority_category
|
137
|
+
|
138
|
+
validates :title, presence: true
|
139
|
+
validates :price, numericality: { only_integer: true },presence: true
|
140
|
+
end
|
141
|
+
```
|
142
|
+
priority_category.rb
|
143
|
+
```ruby
|
144
|
+
class Priority_category < ApplicationRecord
|
145
|
+
has_many :targets
|
146
|
+
end
|
147
|
+
|
148
|
+
```
|
149
|
+
category.rb
|
150
|
+
```ruby
|
151
|
+
class Category < ApplicationRecord
|
152
|
+
has_many :targets
|
153
|
+
end
|
154
|
+
|
129
155
|
```
|
3
コードの変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -20,6 +20,7 @@
|
|
20
20
|
コントローラーの記述は以下の通りです。(targets_controller.rb)
|
21
21
|
```ruby
|
22
22
|
class TargetsController < ApplicationController
|
23
|
+
# before_action :move_to_index, only: [:new]
|
23
24
|
|
24
25
|
def index
|
25
26
|
|
@@ -59,11 +60,15 @@
|
|
59
60
|
|
60
61
|
private
|
61
62
|
def target_params
|
62
|
-
params.require(:target).permit(:title, :
|
63
|
+
params.require(:target).permit(:title, :price, :date, :memo, :created_at).merge(targets_category: category.name,priority: priority_category.name)
|
63
|
-
|
64
|
+
#上記がエラー該当部分です。
|
64
65
|
end
|
65
66
|
|
67
|
+
def move_to_index
|
68
|
+
redirect_to "/"
|
69
|
+
end
|
66
70
|
end
|
71
|
+
|
67
72
|
```
|
68
73
|
新規作成ページの記述は以下の通りです。(new.html.erb)
|
69
74
|
|
@@ -76,8 +81,8 @@
|
|
76
81
|
</div>
|
77
82
|
|
78
83
|
<div class="form-group">
|
79
|
-
<%= form.label :
|
84
|
+
<%= form.label :targets_category, "カテゴリ" %><br>
|
80
|
-
<%= form.select :
|
85
|
+
<%= form.select :targets_category, @category.map{|category|[category.name,category.id]} %>
|
81
86
|
</div>
|
82
87
|
|
83
88
|
<div class="form-group">
|
@@ -86,9 +91,9 @@
|
|
86
91
|
</div>
|
87
92
|
|
88
93
|
<div class="form-group">
|
89
|
-
<%= form.label :
|
94
|
+
<%= form.label :date , "期日" %><br>
|
90
95
|
<%=form.date_select(
|
91
|
-
:
|
96
|
+
:date,
|
92
97
|
use_month_numbers: true,
|
93
98
|
start_year: 1998,
|
94
99
|
end_year: (Time.now.year + 100),
|
@@ -107,9 +112,9 @@
|
|
107
112
|
</div>
|
108
113
|
|
109
114
|
<div class="form-group">
|
110
|
-
<%= form.label :
|
115
|
+
<%= form.label :created_at , "作成日" %><br>
|
111
116
|
<%=form.date_select(
|
112
|
-
:
|
117
|
+
:created_at,
|
113
118
|
use_month_numbers: true,
|
114
119
|
start_year: 1998,
|
115
120
|
end_year: (Time.now.year + 100),
|
2
mergeメソッドを使用してみた情報を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -12,7 +12,8 @@
|
|
12
12
|
※関係あるかわかりませんが、Bootstrapも使用してます。
|
13
13
|
|
14
14
|
## 試してみた事
|
15
|
-
ストロングパラメーターの「permit(:target)」部分を消してみた。=>エラー発生
|
15
|
+
ストロングパラメーターの「permit(:target)」部分を消してみた。=>引き続きエラー発生
|
16
|
+
permitの中の「category」と「priority」を.merge(category:category.name, priority:priority_category.name)と記述を変更してみた。=>変化なし
|
16
17
|
|
17
18
|
##コード記述
|
18
19
|
|
1
データベースの概要を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -8,6 +8,7 @@
|
|
8
8
|
「Unable to autoload constant PriorityCategory, expected /Users/ren0826nosuke/Desktop/projects/target-app/app/models/priority_category.rb to define it」
|
9
9
|
ストロングパラメーター定義部分がエラー該当箇所なのですがどこかおかしな点はありますか?
|
10
10
|
ちなみにform_withを使っています。
|
11
|
+
データベース(モデル)はTargetモデル(目標の情報を入っている)、Categoryモデル(カテゴリの情報が入っている)、Priority_categoryモデル(優先度の情報が入っている)の3つがあります。
|
11
12
|
※関係あるかわかりませんが、Bootstrapも使用してます。
|
12
13
|
|
13
14
|
## 試してみた事
|