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

質問編集履歴

9

エラーの対処法があっているのかの確認

2020/08/18 21:06

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -383,4 +383,10 @@
383
383
 
384
384
  質問ばっかりすいません。
385
385
 
386
- よろしくお願いします。
386
+ よろしくお願いします。
387
+
388
+ 追記⑥
389
+ ![イメージ説明](f0dcbf7f707e1e2b62bb1c0eb163d3d8.png)
390
+
391
+ qiitaやteratailsで探したのですがgemfileの更新などが出てきました。
392
+ やっていいものかわからなかったので確認のため質問します。

8

現場を再度アップします。

2020/08/18 21:06

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -308,4 +308,79 @@
308
308
  これでいいのですか?
309
309
 
310
310
  以下のエラーになりました。
311
- ![イメージ説明](49f04eef69a073f41243f2ff92508712.png)
311
+ ![イメージ説明](49f04eef69a073f41243f2ff92508712.png)
312
+
313
+
314
+ 追記⑤
315
+
316
+ like ?"."#{month}%"がカンマの部分を直して上のエラーは無くなりました。
317
+ 次はNo method errorで止まってしましました。
318
+
319
+ ![イメージ説明](046a5be090255a974d432adf1739d830.png)
320
+
321
+ model family.rb
322
+ ```
323
+ class Family < ApplicationRecord
324
+ belongs_to :user
325
+ attachment :image
326
+
327
+ with_options presence: true do
328
+ validates :category
329
+ validates :molph
330
+ validates :image
331
+ validates :fathermolph
332
+ validates :mothermolph
333
+ validates :hatchdate
334
+ validates :sex
335
+ validates :weight
336
+ validates :feature
337
+ end
338
+
339
+ def get_serial
340
+ month = Date.parse(individualnumber+"/1").strftime("%y年%m月-")
341
+ number = Family.where("individualnumber like ?","#{month}%").
342
+ latest = where("individualnumber like ?","#{month}%").
343
+ order(individualnumber: :desc).first
344
+ number = latest ? latest.individualnumber[/月-(\d+)/,1].succ : 1
345
+ self.individualnumber = month + number
346
+ end
347
+ end
348
+ ```
349
+
350
+ families.controller
351
+ ```
352
+ class FamiliesController < ApplicationController
353
+
354
+ 省略
355
+ def create
356
+ @family = Family.new(family_params)
357
+ @family.get_serial
358
+ @family.user_id = current_user.id
359
+ if @family.save
360
+ redirect_to family_path(@family), notice: "登録完了しました。"
361
+ else
362
+ render :new
363
+ end
364
+ end
365
+
366
+ def edit
367
+ @family = Family.find(params[:id])
368
+ if @family.user != current_user
369
+ redirect_to families_path, alert: "不正なアクセスです。"
370
+ end
371
+ end
372
+
373
+ def update
374
+ @family = Family.find(params[:id])
375
+ if @family.update(family_params)
376
+ redirect_to family_path(@family), notice: "更新に成功しました。"
377
+ else
378
+ render :edit
379
+ end
380
+ end
381
+
382
+ ```
383
+
384
+ 質問ばっかりすいません。
385
+
386
+ よろしくお願いします。

7

コメントを入力

2020/08/16 20:42

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -290,4 +290,22 @@
290
290
  self.individualnumber = month + number
291
291
  end
292
292
  end
293
- ```
293
+ ```
294
+
295
+
296
+ 追記④
297
+
298
+ ```modelfamily
299
+ def get_serial
300
+ month = Date.parse(individualnumber+"/1").strftime("%y年%m月-")
301
+ number = Family.where("individualnumber like ?","#{month}%").
302
+ latest = where("individualnumber like ?"."#{month}%").
303
+ order(individualnumber: :desc).first
304
+ number = latest ? latest.individualnumber[/月-(\d+)/,1].succ : 1
305
+ self.individualnumber = month + number
306
+ end
307
+ ```
308
+ これでいいのですか?
309
+
310
+ 以下のエラーになりました。
311
+ ![イメージ説明](49f04eef69a073f41243f2ff92508712.png)

6

familyrbの変更を追記します。

2020/08/16 01:06

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -266,14 +266,28 @@
266
266
  何が違ってますでしょうか。
267
267
 
268
268
  ```familyrb
269
- def create
270
- @family = Family.new(family_params)
269
+ class Family < ApplicationRecord
270
+ belongs_to :user
271
+ attachment :image
272
+
273
+ with_options presence: true do
274
+ validates :category
275
+ validates :molph
271
- @family.get_serial
276
+ validates :image
272
- @family.user_id = current_user.id
277
+ validates :fathermolph
278
+ validates :mothermolph
273
- if @family.save
279
+ validates :hatchdate
274
- redirect_to family_path(@family), notice: "登録完了しました。"
275
- else
276
- render :new
280
+ validates :sex
277
- end
281
+ validates :weight
282
+ validates :feature
278
283
  end
284
+
285
+ def get_serial
286
+ month = Date.parse(individualnumber+"/1").strftime("%y年%m月-")
287
+ number = Family.where("individualnumber like ?","#{month}%").
288
+ order(individualnumber: :desc).
289
+ first.individualnumber[/月-(\d+)/,1].succ
290
+ self.individualnumber = month + number
291
+ end
292
+ end
279
293
  ```

5

最後のmodelのfamilyrbを変更しています。

2020/08/15 22:42

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -265,7 +265,7 @@
265
265
  何を変更していいのやら全くわからないです。
266
266
  何が違ってますでしょうか。
267
267
 
268
- ```familycontroller
268
+ ```familyrb
269
269
  def create
270
270
  @family = Family.new(family_params)
271
271
  @family.get_serial

4

familycontrollerの中身を追記しました。

2020/08/15 22:40

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -263,4 +263,17 @@
263
263
  追記③
264
264
  ![](368ef4ee40acacf73654ed28d01d3ad9.png)
265
265
  何を変更していいのやら全くわからないです。
266
- 何が違ってますでしょうか。
266
+ 何が違ってますでしょうか。
267
+
268
+ ```familycontroller
269
+ def create
270
+ @family = Family.new(family_params)
271
+ @family.get_serial
272
+ @family.user_id = current_user.id
273
+ if @family.save
274
+ redirect_to family_path(@family), notice: "登録完了しました。"
275
+ else
276
+ render :new
277
+ end
278
+ end
279
+ ```

3

エラーに次ぐエラーで申し訳ありません。

2020/08/15 22:38

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -258,4 +258,9 @@
258
258
  ![エラー内容](27056ca2830389daa68aec930c41d31f.png)
259
259
 
260
260
  となります。
261
- methodが定義されていないのでしょうか。。。
261
+ methodが定義されていないのでしょうか。。。
262
+
263
+ 追記③
264
+ ![](368ef4ee40acacf73654ed28d01d3ad9.png)
265
+ 何を変更していいのやら全くわからないです。
266
+ 何が違ってますでしょうか。

2

新しいエラーのため追記します。

2020/08/14 21:59

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -208,4 +208,54 @@
208
208
  ```28
209
209
  number = where("individualnumber like ?"."#{month}%").
210
210
  ```
211
- です。
211
+ です。
212
+
213
+
214
+
215
+
216
+ 追記2
217
+ ```familymodel
218
+ class Family < ApplicationRecord
219
+ belongs_to :user
220
+ attachment :image
221
+
222
+ with_options presence: true do
223
+ validates :category
224
+ validates :molph
225
+ validates :image
226
+ validates :fathermolph
227
+ validates :mothermolph
228
+ validates :hatchdate
229
+ validates :sex
230
+ validates :weight
231
+ validates :feature
232
+ end
233
+
234
+ def get_serial
235
+ month = Date.parse(individualnumber+"/1").strftime("%y年%m月-")
236
+ number = Family.where("individualnumber like ?","#{month}%").
237
+ order(individualnumber: :desc).
238
+ first.individualnumber[/月-(\d+)/,1].succ
239
+ self.individualnumber = month + number
240
+ end
241
+ end
242
+ ```
243
+
244
+ ```familycontroller
245
+ def create
246
+ @family = Family.new(family_params)
247
+ @family = get_serial
248
+ @family.user_id = current_user.id
249
+ if @family.save
250
+ redirect_to family_path(@family), notice: "登録完了しました。"
251
+ else
252
+ render :new
253
+ end
254
+ end
255
+ ```
256
+
257
+ 上記ように変更しました。
258
+ ![エラー内容](27056ca2830389daa68aec930c41d31f.png)
259
+
260
+ となります。
261
+ methodが定義されていないのでしょうか。。。

1

回答いただいた内容で更新するとエラーが発生しましたので追記します。

2020/08/13 21:15

投稿

ahiru3
ahiru3

スコア12

title CHANGED
File without changes
body CHANGED
@@ -175,4 +175,37 @@
175
175
  ```
176
176
 
177
177
 
178
- よろしくお願いします。
178
+ よろしくお願いします。
179
+
180
+
181
+ 追記
182
+
183
+ ```familiescontroller
184
+ def create
185
+ @family = Family.new(family_params)
186
+ @family.get_serial
187
+ @family.user_id = current_user.id
188
+ if @family.save
189
+ redirect_to family_path(@family), notice: "登録完了しました。"
190
+ else
191
+ render :new
192
+ end
193
+ end
194
+
195
+ def get_serial
196
+ month = Date.parse(individualnumber+"/1").strftime("%Y年%m月-")
197
+ number = where("individualnumber like ?"."#{month}%").
198
+ order(individualnumber: :desc).
199
+ first.individualnumber[/月-(\d+)/,1].succ
200
+ self.individualnumber = month + number
201
+ end
202
+ ```
203
+ 上記のように記載しました。
204
+ すると下記のエラーが出ています。
205
+
206
+ app/controllers/families_controller.rb:28: syntax error, unexpected tSTRING_BEG
207
+ app/controllers/families_controller.rb:28: syntax error, unexpected ')', expecting end
208
+ ```28
209
+ number = where("individualnumber like ?"."#{month}%").
210
+ ```
211
+ です。