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

質問編集履歴

4

controllerのsearch_params, User::HobbySearchFormのinitializeを変更しました

2021/10/11 10:05

投稿

annasui
annasui

スコア1

title CHANGED
File without changes
body CHANGED
@@ -229,7 +229,7 @@
229
229
  end
230
230
  end
231
231
  ```
232
- ### controllers/user/hobbies_controller.rb
232
+ ### controllers/user/hobbies_controller.rb(修正後)
233
233
  ```
234
234
  class User::HobbiesController < User::Base
235
235
 

3

controllerのsearch_params, User::HobbySearchFormのinitializeを変更しました

2021/10/11 10:05

投稿

annasui
annasui

スコア1

title CHANGED
File without changes
body CHANGED
@@ -51,13 +51,22 @@
51
51
  end %>
52
52
  <% end %>
53
53
  ```
54
- ### form/user/hobby_search_form.rb
54
+ ### form/user/hobby_search_form.rb(修正後)
55
55
  ```
56
56
  class User::HobbySearchForm
57
57
  include ActiveModel::Model
58
58
 
59
59
  attr_accessor :name, :initial_investment, :running_cost, :frequency, :place, :number
60
60
 
61
+ def initialize(search_params = {})
62
+ @name = search_params[:name]
63
+ @initial_investment = search_params[:initial_investment]
64
+ @running_cost = search_params[:running_cost]
65
+ @frequency = search_params[:frequency]
66
+ @place = search_params[:place]
67
+ @number = search_params[:number]
68
+ end
69
+
61
70
  def search
62
71
  rel = Hobby
63
72
 
@@ -254,9 +263,7 @@
254
263
  end
255
264
 
256
265
  def search_params
257
- params[:search]&.permit([
258
- :name, :initial_investment, :running_cost, :frequency, :place, :number
266
+ params.permit(:search).permit(:name, :initial_investment, :running_cost, :frequency, :place, :number)
259
- ])
260
267
  end
261
268
  end
262
269
  ```

2

検索した時にコマンドに表示されるログを追記しました

2021/10/11 10:04

投稿

annasui
annasui

スコア1

title CHANGED
File without changes
body CHANGED
@@ -259,4 +259,6 @@
259
259
  ])
260
260
  end
261
261
  end
262
- ```
262
+ ```
263
+ ### 検索時のログ
264
+ Hobby Load (0.6ms) SELECT "hobbies".* FROM "hobbies" WHERE "hobbies"."name" = $1 ORDER BY "hobbies"."id" ASC [["name", "ランニング"]]

1

すみません、controllerを忘れていました

2021/10/11 01:15

投稿

annasui
annasui

スコア1

title CHANGED
File without changes
body CHANGED
@@ -212,10 +212,51 @@
212
212
  end
213
213
  end
214
214
  end
215
+
215
216
  end
216
217
 
217
218
  def decorated_label(name, label_text, options = {})
218
219
  label(name, label_text, class: options[:required] ? "required" : nil)
219
220
  end
220
221
  end
222
+ ```
223
+ ### controllers/user/hobbies_controller.rb
224
+ ```
225
+ class User::HobbiesController < User::Base
226
+
227
+
228
+ def index
229
+ @search_form = User::HobbySearchForm.new(search_params)
230
+ @hobbies = @search_form.search
231
+ end
232
+
233
+ def new
234
+ @hobby = Hobby.new
235
+ end
236
+
237
+ def create
238
+ @hobby = Hobby.new(hobby_params)
239
+ if @hobby.save
240
+ redirect_to user_hobby_path(@hobby.id), notice: "Hobby #{@hobby.name}を登録しました"
241
+ else
242
+ render "new"
243
+ end
244
+ end
245
+
246
+ def show
247
+ @hobby = Hobby.find_by(params[:hobby_id])
248
+ end
249
+
250
+ private
251
+
252
+ def hobby_params
253
+ params.permit(:name, :initial_investment, :running_cost, :frequency, :place, :number)
254
+ end
255
+
256
+ def search_params
257
+ params[:search]&.permit([
258
+ :name, :initial_investment, :running_cost, :frequency, :place, :number
259
+ ])
260
+ end
261
+ end
221
262
  ```