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

質問編集履歴

7

指摘箇所の修正

2020/08/31 14:08

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -60,7 +60,7 @@
60
60
 
61
61
  def index
62
62
  @posts = Post.all
63
- @category_parent_arrays = Category.where(ancestry: nil)
63
+ @category_parent_array = Category.where(ancestry: nil)
64
64
  end
65
65
 
66
66
  def show
@@ -111,7 +111,7 @@
111
111
 
112
112
  private
113
113
  def post_params
114
- params.require(:post).permit(:title, :body, :image)
114
+ params.require(:post).permit(:title, :body, :image, { post_categories: {} })
115
115
  end
116
116
 
117
117
  def set_category
@@ -146,14 +146,15 @@
146
146
 
147
147
  has_many :post_categories, dependent: :destroy
148
148
  has_many :categories, through: :post_categories
149
+ accepts_nested_attributes_for :categories, allow_destroy: true
149
150
  end
150
151
  ```
151
152
  category.rb
152
153
  ```
153
154
  class Category < ApplicationRecord
154
- has_many :post_category, dependent: :destroy
155
- has_many :posts, through: :post_category
156
155
  has_ancestry
156
+ has_many :post_categories, dependent: :destroy
157
+ has_many :posts, through: :post_categories
157
158
  end
158
159
  ```
159
160
  post_category.rb

6

内容の修正

2020/08/31 14:08

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -1,17 +1,15 @@
1
1
  ### 前提・実現したいこと
2
2
  新規投稿をするときに、セレクトボックスを用いて、カテゴリー機能を実装したいです。
3
3
 
4
- ##なぜカテゴリーもDBに登録したいか
5
- トップページから投稿を検索する機能をこれから実装するにあたり、カテゴリーから検索できるようにするためです。
6
- そのためには、投稿にカテゴリーのデータも必要だからです。
7
-
8
4
  ##状況
9
5
  railsで個人アプリを開発中で、スノボー版インスタグラムを作成しています。
10
6
 
11
- 現在の状況としては、投稿機能を実装し、カテゴリー以外のデータDBに値が取得されるようになっています。
7
+ 現在の状況としては、投稿機能カテゴリー以外は実装されています。
12
8
  (image,text,titleなど)
13
9
 
10
+ また、中間テーブルであるpost_categoryテーブルの紐付けがうまく行っていないのか、新規投稿される時にpost_idとcategory_idはデータベースに登録されずです。
11
+
14
- 以下hがビューの画像です。
12
+ 以下がビューの画像です。
15
13
  ![イメージ説明](04c4f2fefd61e3d1e161cb94082b9cff.png)
16
14
 
17
15
  ### 該当のソースコード
@@ -178,4 +176,8 @@
178
176
  {name: "BURTON"},
179
177
  {name: "DEELUXE"},
180
178
  {name: "FLUX"}])
181
- ```
179
+ ```
180
+
181
+ 足りない情報があれば教えてください。
182
+
183
+ なんでカテゴリーが登録されないんでしょうか?

5

コード内容の修正

2020/08/30 11:03

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,5 @@
1
1
  ### 前提・実現したいこと
2
- 新規投稿をするときに、カテゴリーをデータベーに保存し投稿にカテゴリーのデータせたい。
2
+ 新規投稿をするときに、セレクトボックを用いて、カテゴリー機能実装したいです
3
- 現状は、投稿したときに、カテゴリー以外の情報はDBに登録される。
4
3
 
5
4
  ##なぜカテゴリーもDBに登録したいか
6
5
  トップページから投稿を検索する機能をこれから実装するにあたり、カテゴリーから検索できるようにするためです。
@@ -51,119 +50,19 @@
51
50
  .field-label
52
51
  = f.label :category, "カテゴリーを選択してください"
53
52
  .field-input
54
- = render partial: "category"
53
+ = f.select :category_ids, options_for_select( @category_parent_array.map{|c| [c, {}]}), {}, {class: "parent_select", id: "parent_category"}
55
54
  .actions
56
55
  = f.submit "投稿する", class: "btn"
57
56
  ```
58
- _category.html.haml
57
+ posts_cotllor.rb
59
58
  ```
60
- .categorySelect
61
- %select#parent_category
62
- - @parent_categories.each do |c|
63
- %option{value: "parent"}
64
- = c.name
65
-
66
- %select#children_category
67
- - @default_child_categories.each do |c|
68
- %option{value: "children"}
69
- = c.name
70
- ```
71
-
72
- mysqlのデータベース関連
73
- ![イメージ説明](9d774ee916e60bbedacfb10a141a7690.png)
74
- ![イメージ説明](352c5fa3fc51298a580804e83c084556.png)
75
- ![イメージ説明](bbe76a21beea3ab5254aa2039629aab0.png)
76
-
77
- ##アソシエーション
78
- post.rb
79
- ```
80
- class Post < ApplicationRecord
81
- belongs_to :user
82
- attachment :image
83
- has_many :favorites, dependent: :destroy
84
- has_many :comments
85
-
86
- with_options presence: true do
87
- validates :title
88
- validates :body
89
- validates :image
90
- end
91
-
92
- has_many :post_categories, dependent: :destroy
93
- has_many :categories, through: :post_categories
94
- end
95
- ```
96
- category.rb
97
- ```
98
- class Category < ApplicationRecord
99
- has_many :post_category, dependent: :destroy
100
- has_many :posts, through: :post_category
101
- has_ancestry
102
- end
103
- ```
104
- post_category.rb
105
- ```
106
- class PostCategory < ApplicationRecord
107
- belongs_to :post
108
- belongs_to :category
109
- end
110
- ```
111
- db/fixtures/01_category.rb
112
- ```
113
- parents = %w[ スノーボード ブーツ ビンディング ]
114
- snowboard_child = %w[ BURTON K2 SALOMON ]
115
- snowboots_child = %w[ BURTON DEELUXE FLUX ]
116
- binding_child = %w[ BURTON UNION FLUX ]
117
-
118
- parents.each do |parent|
119
- Category.seed do |c|
120
- c.name = parent
121
- end
122
- end
123
-
124
- snowboard_child.each do |child|
125
- Category.find_by!(name: 'スノーボード').children.create(name: child)
126
- end
127
-
128
- snowboots_child.each do |child|
129
- Category.find_by!(name: 'ブーツ').children.create(name: child)
130
- end
131
-
132
- binding_child.each do |child|
133
- Category.find_by!(name: 'ビンディング').children.create(name: child)
134
- end
135
- ```
136
- ##試したこと
137
- _form.html.hamlにて、以下のようにf.selectを使ってカテゴリーをデータベースに登録しようとしたが、
138
- category_idsの後に配列を組まなければならず、コードが冗長になってしまう。
139
-
140
- また、試しにこの方法で簡易的なデータを入力してデータベースに登録されているか確認したが、データベースにカテゴリーが登録されていなかった。
141
- ```
142
- .field
143
- .field-label
144
- = f.label :category, "カテゴリーを選択してください"
145
- .field-input
146
- = f.select :category_ids ##追記
147
- = render partial: "category"
148
- ```
149
- もしf.selectを使って、上記であげた部分テンプレートや01_category.rbにあるカテゴリー配列・定数を使ってデータベースに登録できる表記があればご教示ください。
150
- ※カテゴリーの配列は、この10倍ぐらいあります。なので、_form.html.hamlに直接書くのは好ましくないと思い、部分テンプレートなどを使っています。
151
- そのため、もし、f.select :category_idsの後に記述する方法があればお願いいたします。
152
-
153
- そのほかにも先輩方のカテゴリー登録できる方法があればご教示いただきたいです。
154
-
155
- 足りない情報があればお申し付けください。
156
-
157
- 【追記】
158
- @parent_categories. @default_child_categoriesの変数について
159
-
160
- posts_cotroller.rb
161
- ```
162
59
  class PostsController < ApplicationController
163
60
  before_action :authenticate_user!, except: [:index]
164
- before_action :set_categories, only: [:new, :create]
61
+ before_action :set_category, only:[:new, :create]
62
+
165
63
  def index
166
64
  @posts = Post.all
65
+ @category_parent_arrays = Category.where(ancestry: nil)
167
66
  end
168
67
 
169
68
  def show
@@ -176,6 +75,10 @@
176
75
  @post = Post.new
177
76
  end
178
77
 
78
+ def get_category_children
79
+ @category_children = Category.find("#{params[parent_id]}").children
80
+ end
81
+
179
82
  def create
180
83
  @post = Post.new(post_params)
181
84
  @post.user_id = current_user.id
@@ -208,51 +111,71 @@
208
111
  redirect_to posts_path
209
112
  end
210
113
 
211
- def dynamic_select_category
212
- @category = Category.find(params[:category_id])
213
- end
214
-
215
114
  private
216
115
  def post_params
217
116
  params.require(:post).permit(:title, :body, :image)
218
117
  end
219
118
 
220
- def set_categories
119
+ def set_category
120
+ @category_parent_array = []
121
+ Category.where(ancestry: nil).each do |parent|
221
- @category_pararent = Category.where(ancestry: nil)
122
+ @category_parent_array << parent.name
123
+ end
222
124
  end
223
-
224
125
  end
225
126
 
226
127
  ```
227
128
 
129
+ mysqlのデータベース関連
130
+ ![イメージ説明](9d774ee916e60bbedacfb10a141a7690.png)
131
+ ![イメージ説明](352c5fa3fc51298a580804e83c084556.png)
132
+ ![イメージ説明](bbe76a21beea3ab5254aa2039629aab0.png)
133
+
134
+ ##アソシエーション
135
+ post.rb
228
136
  ```
229
- = form_for(@post) do |f|
230
- .field
231
- .field-label
232
- = f.label :title, "snowB"
137
+ class Post < ApplicationRecord
233
- .field-input
234
- = f.text_field :title
235
- .field
236
- .field-label
237
- = f.label :body, "ココがポイント!"
238
- .field-input
239
- = f.text_area :body
138
+ belongs_to :user
240
- .field
241
- .field-label
242
- = f.label :image, "snowB写真"
243
- .field-input
244
- = f.attachment_field :image
139
+ attachment :image
140
+ has_many :favorites, dependent: :destroy
141
+ has_many :comments
142
+
143
+ with_options presence: true do
144
+ validates :title
145
+ validates :body
146
+ validates :image
245
- .field
147
+ end
148
+
246
- .field-label
149
+ has_many :post_categories, dependent: :destroy
247
- = f.label :category, "カテゴリーを選択してください"
150
+ has_many :categories, through: :post_categories
248
- .field-input
249
- = f.fields_for :categories do |c|
250
- = c.select :category, options_for_select(@category_parent.map{|c| [c[:name], c[:id]]}), {class: "parant_category_box", id: "parent_category"}
251
- .actions
151
+ end
252
- = f.submit "投稿する", class: "btn"
253
152
  ```
153
+ category.rb
154
+ ```
155
+ class Category < ApplicationRecord
156
+ has_many :post_category, dependent: :destroy
157
+ has_many :posts, through: :post_category
158
+ has_ancestry
159
+ end
160
+ ```
161
+ post_category.rb
162
+ ```
163
+ class PostCategory < ApplicationRecord
164
+ belongs_to :post
165
+ belongs_to :category
166
+ end
167
+ ```
168
+ db/seeds.rb
169
+ ```
170
+ snowboard = Category.create(name: "スノーボード")
171
+ snowboard_1 = snowboard.children.create([
172
+ {name: "BURTON"},
173
+ {name: "K2"},
174
+ {name: "SALOMON"}])
254
175
 
255
- 参考記事:
256
- https://qiita.com/manbolila/items/7c44142de50093470580
257
-
258
- ![イメージ説明](864e6e09ea2036911b7422c4772bfaab.png)
176
+ snowboots = Category.create(name: "ブーツ")
177
+ snowboots_1 = snowboots.children.create([
178
+ {name: "BURTON"},
179
+ {name: "DEELUXE"},
180
+ {name: "FLUX"}])
181
+ ```

4

タイトルの変更

2020/08/30 10:57

投稿

susume
susume

スコア13

title CHANGED
@@ -1,1 +1,1 @@
1
- カテゴリーをDBに登録し、投稿にカテゴリーデータせた
1
+ 新規投稿にセレクトボックスを用いて、カテゴリー機能実装したい
body CHANGED
File without changes

3

options_forなどを記載

2020/08/30 10:53

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -161,7 +161,7 @@
161
161
  ```
162
162
  class PostsController < ApplicationController
163
163
  before_action :authenticate_user!, except: [:index]
164
- before_action :set_categories, only: [:edit, :new]
164
+ before_action :set_categories, only: [:new, :create]
165
165
  def index
166
166
  @posts = Post.all
167
167
  end
@@ -218,11 +218,41 @@
218
218
  end
219
219
 
220
220
  def set_categories
221
- @parent_categories = Category.where(ancestry: nil)
221
+ @category_pararent = Category.where(ancestry: nil)
222
- @default_child_categories = @parent_categories.first.children
223
222
  end
224
223
 
225
224
  end
226
225
 
227
226
  ```
227
+
228
- a
228
+ ```
229
+ = form_for(@post) do |f|
230
+ .field
231
+ .field-label
232
+ = f.label :title, "snowB"
233
+ .field-input
234
+ = f.text_field :title
235
+ .field
236
+ .field-label
237
+ = f.label :body, "ココがポイント!"
238
+ .field-input
239
+ = f.text_area :body
240
+ .field
241
+ .field-label
242
+ = f.label :image, "snowB写真"
243
+ .field-input
244
+ = f.attachment_field :image
245
+ .field
246
+ .field-label
247
+ = f.label :category, "カテゴリーを選択してください"
248
+ .field-input
249
+ = f.fields_for :categories do |c|
250
+ = c.select :category, options_for_select(@category_parent.map{|c| [c[:name], c[:id]]}), {class: "parant_category_box", id: "parent_category"}
251
+ .actions
252
+ = f.submit "投稿する", class: "btn"
253
+ ```
254
+
255
+ 参考記事:
256
+ https://qiita.com/manbolila/items/7c44142de50093470580
257
+
258
+ ![イメージ説明](864e6e09ea2036911b7422c4772bfaab.png)

2

初心者マークの追加

2020/08/30 07:31

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -224,4 +224,5 @@
224
224
 
225
225
  end
226
226
 
227
- ```
227
+ ```
228
+ a

1

コントローラーの追記

2020/08/30 04:08

投稿

susume
susume

スコア13

title CHANGED
File without changes
body CHANGED
@@ -152,4 +152,76 @@
152
152
 
153
153
  そのほかにも先輩方のカテゴリー登録できる方法があればご教示いただきたいです。
154
154
 
155
- 足りない情報があればお申し付けください。
155
+ 足りない情報があればお申し付けください。
156
+
157
+ 【追記】
158
+ @parent_categories. @default_child_categoriesの変数について
159
+
160
+ posts_cotroller.rb
161
+ ```
162
+ class PostsController < ApplicationController
163
+ before_action :authenticate_user!, except: [:index]
164
+ before_action :set_categories, only: [:edit, :new]
165
+ def index
166
+ @posts = Post.all
167
+ end
168
+
169
+ def show
170
+ @post = Post.find(params[:id])
171
+ @comment = Comment.new
172
+ @comments = @post.comments.includes(:user)
173
+ end
174
+
175
+ def new
176
+ @post = Post.new
177
+ end
178
+
179
+ def create
180
+ @post = Post.new(post_params)
181
+ @post.user_id = current_user.id
182
+ if @post.save
183
+ redirect_to post_path(@post), notice: '投稿されました'
184
+ else
185
+ render :new, alert: '投稿できませんでした'
186
+ end
187
+ end
188
+
189
+ def edit
190
+ @post = Post.find(params[:id])
191
+ if @post.user != current_user
192
+ redirect_to posts_path, alert: '不正なアクセスです'
193
+ end
194
+ end
195
+
196
+ def update
197
+ @post = Post.find(params[:id])
198
+ if @post.update(post_params)
199
+ redirect_to post_path(@post), notice: '投稿が更新されました'
200
+ else
201
+ render :edit
202
+ end
203
+ end
204
+
205
+ def destroy
206
+ post = Post.find(params[:id])
207
+ post.destroy
208
+ redirect_to posts_path
209
+ end
210
+
211
+ def dynamic_select_category
212
+ @category = Category.find(params[:category_id])
213
+ end
214
+
215
+ private
216
+ def post_params
217
+ params.require(:post).permit(:title, :body, :image)
218
+ end
219
+
220
+ def set_categories
221
+ @parent_categories = Category.where(ancestry: nil)
222
+ @default_child_categories = @parent_categories.first.children
223
+ end
224
+
225
+ end
226
+
227
+ ```