質問編集履歴
2
初心者マークに変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,102 +1,124 @@
|
|
1
1
|
##### コントローラー(recipe)
|
2
2
|
|
3
|
+
```ruby
|
4
|
+
|
5
|
+
app/controllers/recipes_controller.rb
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
class RecipesController < ApplicationController
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
def index
|
14
|
+
|
15
|
+
@recipe = Recipe.all.order(id: 'DESC')
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
def new
|
22
|
+
|
23
|
+
@recipe = Recipe.new
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
def create
|
30
|
+
|
31
|
+
@recipe = Recipe.new(recipe_params)
|
32
|
+
|
33
|
+
if @recipe.save
|
34
|
+
|
35
|
+
redirect_to root_path
|
36
|
+
|
37
|
+
else
|
38
|
+
|
39
|
+
render :new
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
def recipe_params
|
52
|
+
|
53
|
+
params.require(:recipe).permit(:image, :name, :genre_id, :food, :seasoning, :procedure).merge(user_id: current_user.id)
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
##### モデル(recipe)
|
64
|
+
|
3
65
|
```ruby
|
4
66
|
|
5
|
-
app/
|
67
|
+
app/models/recipe.rb
|
6
|
-
|
7
|
-
|
8
|
-
|
68
|
+
|
69
|
+
|
70
|
+
|
9
|
-
class Recipe
|
71
|
+
class Recipe < ApplicationRecord
|
72
|
+
|
73
|
+
# バリデーション
|
10
74
|
|
11
75
|
|
12
76
|
|
13
|
-
def index
|
14
|
-
|
15
|
-
@recipe = Recipe.all.order(id: 'DESC')
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
def new
|
22
|
-
|
23
|
-
@recipe = Recipe.new
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
def create
|
30
|
-
|
31
|
-
@recipe = Recipe.new(recipe_params)
|
32
|
-
|
33
|
-
|
77
|
+
# 空のカラムは保存ができない
|
34
|
-
|
35
|
-
|
78
|
+
|
36
|
-
|
37
|
-
else
|
38
|
-
|
39
|
-
render :new
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
def recipe_params
|
52
|
-
|
53
|
-
|
79
|
+
validates :name, :image, :genre, :food, :seasoning, :procedure, presence: true
|
80
|
+
|
54
|
-
|
81
|
+
# ジャンルの選択が「---」の時は保存できないようにする
|
82
|
+
|
83
|
+
validates :genre_id, numericality: { other_than: 1 }
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
# アソシエーション
|
88
|
+
|
89
|
+
|
90
|
+
|
55
|
-
en
|
91
|
+
belongs_to :user
|
92
|
+
|
56
|
-
|
93
|
+
has_one_attached :image
|
94
|
+
|
95
|
+
|
96
|
+
|
57
|
-
|
97
|
+
extend ActiveHash::Associations::ActiveRecordExtensions
|
98
|
+
|
99
|
+
belongs_to_active_hash :genre
|
58
100
|
|
59
101
|
end
|
60
102
|
|
103
|
+
|
104
|
+
|
61
|
-
```
|
105
|
+
```
|
62
|
-
|
106
|
+
|
63
|
-
##### モデル(re
|
107
|
+
##### モデル(genre)
|
64
108
|
|
65
109
|
```ruby
|
66
110
|
|
67
|
-
app/models/re
|
111
|
+
app/models/genre.rb
|
68
|
-
|
69
|
-
|
70
|
-
|
112
|
+
|
113
|
+
|
114
|
+
|
71
|
-
class
|
115
|
+
class Genre < ActiveHash::Base
|
72
|
-
|
73
|
-
|
116
|
+
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
# 空のカラムは保存ができない
|
78
|
-
|
79
|
-
validates :name, :image, :genre, :food, :seasoning, :procedure, presence: true
|
80
|
-
|
81
|
-
# ジャンルの選択が「---」の時は保存できないようにする
|
82
|
-
|
83
|
-
validates :genre_id, numericality: { other_than: 1 }
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
# アソシエーション
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
117
|
+
self.data = [
|
92
|
-
|
93
|
-
|
118
|
+
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
119
|
+
{id: 1, name: '---'}, {id: 2, name: '和食'}, {id: 3, name: '洋食'}, {id: 4, name: '中華料理'}
|
98
|
-
|
120
|
+
|
99
|
-
|
121
|
+
]
|
100
122
|
|
101
123
|
end
|
102
124
|
|
@@ -104,28 +126,6 @@
|
|
104
126
|
|
105
127
|
```
|
106
128
|
|
107
|
-
##### モデル(genre)
|
108
|
-
|
109
|
-
```ruby
|
110
|
-
|
111
|
-
app/models/genre.rb
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
class Genre < ActiveHash::Base
|
116
|
-
|
117
|
-
self.data = [
|
118
|
-
|
119
|
-
{id: 1, name: '---'}, {id: 2, name: '和食'}, {id: 3, name: '洋食'}, {id: 4, name: '中華料理'}
|
120
|
-
|
121
|
-
]
|
122
|
-
|
123
|
-
end
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
129
|
|
130
130
|
|
131
131
|
### 前提・実現したいこと
|
1
補足情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -242,4 +242,4 @@
|
|
242
242
|
|
243
243
|
|
244
244
|
|
245
|
-
レシピのcreateアクションまではできていてデーターベースにも保存がされていることを確認。
|
245
|
+
レシピのcreateアクションまではできていてデーターベースにも保存がされていることを確認。genreモデルはactive_hashにてアソシエーション済み。
|