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

質問編集履歴

3

createメソッドのcreateをnewに変更

2020/10/23 01:24

投稿

yozakura10
yozakura10

スコア8

title CHANGED
File without changes
body CHANGED
@@ -80,7 +80,7 @@
80
80
  end
81
81
 
82
82
  def create
83
- @prototype = Prototype.create(prototype_params)
83
+ @prototype = Prototype.*new*(prototype_params)
84
84
  if @prototype.save
85
85
  redirect_to root_path
86
86
  else

2

saveに!をつけたところこのようなエラー文が出ました、

2020/10/23 01:24

投稿

yozakura10
yozakura10

スコア8

title CHANGED
File without changes
body CHANGED
@@ -9,24 +9,44 @@
9
9
  エラーメッセージ
10
10
 
11
11
 
12
- ターミナルのエラー文です
12
+ エラー文です
13
13
  ```
14
- Started POST "/prototypes" for ::1 at 2020-10-22 23:26:18 +0900
15
- (0.2ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
16
- Processing by PrototypesController#create as HTML
17
- Parameters: {"authenticity_token"=>"ztX8p9+qIFuWU2w/gpWI45xQf3Rh1OCywI2OUdYpTaOEwVquHTnCBoe9B971OA7XmJvBlGjtVplKKV0BErOaWQ==", "prototype"=>{"title"=>"aaa", "catct_copy"=>"aaa", "concept"=>"aaa"}, "commit"=>"保存する"}
18
- User Load (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
19
- (0.2ms) BEGIN
20
- app/controllers/prototypes_controller.rb:12:in `create'
21
- User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
22
- ↳ app/controllers/prototypes_controller.rb:12:in `create'
23
- (0.1ms) ROLLBACK
24
- ↳ app/controllers/prototypes_controller.rb:12:in `create'
25
- Rendering prototypes/new.html.erb within layouts/application
26
- Rendered prototypes/_form.html.erb (Duration: 0.7ms | Allocations: 546)
27
- Rendered prototypes/new.html.erb within layouts/application (Duration: 0.8ms | Allocations: 617)
28
- [Webpacker] Everything's up-to-date. Nothing to do
29
- Completed 200 OK in 26ms (Views: 20.3ms | ActiveRecord: 0.7ms | Allocations: 14372)
14
+ ActiveRecord::RecordInvalid in PrototypesController#create
15
+ Validation failed: Image can't be blank
16
+ Extracted source (around line #13):
17
+
18
+ def create
19
+ @prototype = Prototype.create(prototype_params)
20
+ if @prototype.save!
21
+ redirect_to root_path
22
+ else
23
+ render :new
24
+
25
+ Rails.root: /Users/kamiyaryota/Desktop/projects/protospace-31035
26
+
27
+ Application Trace | Framework Trace | Full Trace
28
+ app/controllers/prototypes_controller.rb:13:in `create'
29
+ Request
30
+ Parameters:
31
+
32
+ {"authenticity_token"=>"X/fnWlXUYadtT1MdGuTVyinnpu2B/riCk3iqiFXD3HMV40FTl0eD+nyhOPxtSVP+LSwYDYjHDqkZ3HnYkVkLiQ==",
33
+ "prototype"=>
34
+ {"title"=>"aaa",
35
+ "catct_copy"=>"aaa",
36
+ "concept"=>"aaaa",
37
+ "image"=>
38
+ #<ActionDispatch::Http::UploadedFile:0x00007fe8ead15300
39
+ @content_type="image/png",
40
+ @headers="Content-Disposition: form-data; name=\"prototype[image]\"; filename=\"test_image.png\"\r\n" + "Content-Type: image/png\r\n",
41
+ @original_filename="test_image.png",
42
+ @tempfile=#<File:/var/folders/4y/hbjp90hd2rl1bfd0m4kczr3m0000gn/T/RackMultipart20201023-27843-i4n9cr.png>>},
43
+ "commit"=>"保存する"}
44
+ Toggle session dump
45
+ Toggle env dump
46
+ Response
47
+ Headers:
48
+
49
+ None
30
50
  ```
31
51
 
32
52
 
@@ -79,7 +99,8 @@
79
99
  end
80
100
  ```
81
101
 
82
- model.rbです
102
+ model
103
+ prototype.rbです
83
104
  ```ryby
84
105
  class Prototype < ApplicationRecord
85
106
  has_many :comments
@@ -95,7 +116,26 @@
95
116
  end
96
117
  ```
97
118
 
119
+ model
120
+ user.rbです
98
121
 
122
+ ```ruby
123
+ class User < ApplicationRecord
124
+ # Include default devise modules. Others available are:
125
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
126
+ devise :database_authenticatable, :registerable,
127
+ :recoverable, :rememberable, :validatable
128
+
129
+ has_many :prototypes
130
+ has_many :comments
131
+
132
+ validates :name, presence: true
133
+ validates :profile, presence: true
134
+ validates :occupation, presence: true
135
+ validates :position, presence: true
136
+ end
137
+ ```
138
+
99
139
  *投稿ページです*
100
140
  ```html
101
141
  <div class="main">

1

codeの修正

2020/10/23 01:20

投稿

yozakura10
yozakura10

スコア8

title CHANGED
File without changes
body CHANGED
@@ -26,7 +26,8 @@
26
26
  Rendered prototypes/_form.html.erb (Duration: 0.7ms | Allocations: 546)
27
27
  Rendered prototypes/new.html.erb within layouts/application (Duration: 0.8ms | Allocations: 617)
28
28
  [Webpacker] Everything's up-to-date. Nothing to do
29
- Completed 200 OK in 26ms (Views: 20.3ms | ActiveRecord: 0.7ms | Allocations: 14372)```
29
+ Completed 200 OK in 26ms (Views: 20.3ms | ActiveRecord: 0.7ms | Allocations: 14372)
30
+ ```
30
31
 
31
32
 
32
33
  該当コード