質問編集履歴

2

rails cでのデータ保存

2020/08/30 02:53

投稿

HaTea
HaTea

スコア16

test CHANGED
File without changes
test CHANGED
@@ -258,7 +258,51 @@
258
258
 
259
259
  ```
260
260
 
261
-
261
+ ```
262
+
263
+ ログ←rails c 追記
264
+
265
+ from /Users/hatea/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query'
266
+
267
+ [4] pry(main)> User.create(nickname: "hoge", email: "example@fuga.com", password: "123456", password_confirmation: "123456",team_id: "2",favorite_player: "",introduction: "")
268
+
269
+ (0.2ms) BEGIN
270
+
271
+ User Exists? (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'example@fuga.com' LIMIT 1
272
+
273
+ User Create (0.2ms) INSERT INTO `users` (`nickname`, `team_id`, `favorite_player`, `introduction`, `email`, `encrypted_password`, `created_at`, `updated_at`) VALUES ('hoge', 2, '', '', 'example@fuga.com', '$2a$12$dWYbTILaK0K6lKLiwYRIme9Ugc83KZqeR/2ko.vipKc2Eco7YQbi6', '2020-08-30 02:35:32.726778', '2020-08-30 02:35:32.726778')
274
+
275
+ (0.9ms) COMMIT
276
+
277
+ => #<User id: 2, nickname: "hoge", team_id: 2, favorite_player: "", introduction: "", email: "example@fuga.com", created_at: "2020-08-30 02:35:32", updated_at: "2020-08-30 02:35:32">
278
+
279
+
280
+
281
+ [8] pry(main)> Room.create(name: "ルーム1", user_ids:1, content:"")
282
+
283
+ User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
284
+
285
+ (0.2ms) BEGIN
286
+
287
+ Room Create (0.2ms) INSERT INTO `rooms` (`name`, `content`, `created_at`, `updated_at`) VALUES ('ルーム1', '', '2020-08-30 02:38:29.885077', '2020-08-30 02:38:29.885077')
288
+
289
+ RoomUser Create (0.2ms) INSERT INTO `room_users` (`room_id`, `user_id`, `created_at`, `updated_at`) VALUES (9, 1, '2020-08-30 02:38:29.886483', '2020-08-30 02:38:29.886483')
290
+
291
+ (0.5ms) COMMIT
292
+
293
+ => #<Room:0x00007ff5eb34a650
294
+
295
+ id: 9,
296
+
297
+ name: "ルーム1",
298
+
299
+ content: "",
300
+
301
+ created_at: Sun, 30 Aug 2020 11:38:29 JST +09:00,
302
+
303
+ updated_at: Sun, 30 Aug 2020 11:38:29 JST +09:00>
304
+
305
+ ```
262
306
 
263
307
  ### 試したこと
264
308
 

1

保存するコードとログの追記

2020/08/30 02:53

投稿

HaTea
HaTea

スコア16

test CHANGED
File without changes
test CHANGED
@@ -58,6 +58,8 @@
58
58
 
59
59
  ```
60
60
 
61
+
62
+
61
63
  ```ruby
62
64
 
63
65
  models/user.rb
@@ -116,6 +118,146 @@
116
118
 
117
119
  ```
118
120
 
121
+ ```html
122
+
123
+ index.html.erb ←追記
124
+
125
+
126
+
127
+ <%=form_with model: @room, local: true do |f|%>
128
+
129
+ <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
130
+
131
+ <div class='chat-room-form__field--right'>
132
+
133
+ <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
134
+
135
+ </div>
136
+
137
+ <%= f.label :内容, class: 'chat-room-form__label'%>
138
+
139
+ <div class='chat-room-form__field--right'>
140
+
141
+ <%= f.text_area :content, class: 'chat__room_name chat-room-form__input', size: "30×30"%>
142
+
143
+ </div>
144
+
145
+ <%= f.submit class: 'chat-room-form__action-btn'%>
146
+
147
+ <%end%>
148
+
149
+ ```
150
+
151
+ ```ruby
152
+
153
+ rooms_controller←追記
154
+
155
+
156
+
157
+ class RoomsController < ApplicationController
158
+
159
+ def index
160
+
161
+ @room = Room.new
162
+
163
+ @rooms = Room.all.order(id: "DESC")
164
+
165
+ end
166
+
167
+ def create
168
+
169
+ @room = Room.new(room_params)
170
+
171
+ if @room.save
172
+
173
+ redirect_to root_path
174
+
175
+ else
176
+
177
+ render :inde
178
+
179
+ end
180
+
181
+ end
182
+
183
+ def destroy
184
+
185
+ room = Room.find(params[:id])
186
+
187
+ room.destroy
188
+
189
+ redirect_to root_path
190
+
191
+ end
192
+
193
+
194
+
195
+ private
196
+
197
+ def room_params
198
+
199
+ params.require(:room).permit(:name,:content, user_ids:[])
200
+
201
+ end
202
+
203
+ end
204
+
205
+ ```
206
+
207
+ ```
208
+
209
+ ログ←追記
210
+
211
+ Started POST "/rooms" for ::1 at 2020-08-29 17:15:37 +0900
212
+
213
+ Processing by RoomsController#create as HTML
214
+
215
+ Parameters: {"authenticity_token"=>"MDb1owe+4mdaAauyjwGJquTMrrtKUd1fbinr0e3nbBxD65/ZhUaDJjkSYNV4eMDIWFs3YNqrf1UzKCRqboJZVg==", "room"=>{"name"=>"テストチャットルーム", "content"=>"test"}, "commit"=>"Create Room"}
216
+
217
+ (0.1ms) BEGIN
218
+
219
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
220
+
221
+ Room Create (0.3ms) INSERT INTO `rooms` (`name`, `content`, `created_at`, `updated_at`) VALUES ('テストチャットルーム', 'test', '2020-08-29 08:15:37.661242', '2020-08-29 08:15:37.661242')
222
+
223
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
224
+
225
+ (0.3ms) COMMIT
226
+
227
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
228
+
229
+ Redirected to http://localhost:3000/
230
+
231
+ Completed 302 Found in 5ms (ActiveRecord: 0.8ms | Allocations: 3370)
232
+
233
+
234
+
235
+
236
+
237
+ Started GET "/" for ::1 at 2020-08-29 17:15:37 +0900
238
+
239
+ Processing by RoomsController#index as HTML
240
+
241
+ Rendering rooms/index.html.erb within layouts/application
242
+
243
+ Rendered shared/_header.html.erb (Duration: 0.0ms | Allocations: 6)
244
+
245
+ User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
246
+
247
+ ↳ app/views/rooms/index.html.erb:9
248
+
249
+ Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` ORDER BY `rooms`.`id` DESC
250
+
251
+ ↳ app/views/rooms/index.html.erb:101
252
+
253
+ Rendered rooms/index.html.erb within layouts/application (Duration: 28.1ms | Allocations: 14430)
254
+
255
+ [Webpacker] Everything's up-to-date. Nothing to do
256
+
257
+ Completed 200 OK in 40ms (Views: 38.6ms | ActiveRecord: 0.6ms | Allocations: 26544)
258
+
259
+ ```
260
+
119
261
 
120
262
 
121
263
  ### 試したこと