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

質問編集履歴

2

rails cでのデータ保存

2020/08/30 02:53

投稿

HaTea
HaTea

スコア16

title CHANGED
File without changes
body CHANGED
@@ -128,7 +128,29 @@
128
128
  [Webpacker] Everything's up-to-date. Nothing to do
129
129
  Completed 200 OK in 40ms (Views: 38.6ms | ActiveRecord: 0.6ms | Allocations: 26544)
130
130
  ```
131
+ ```
132
+ ログ←rails c 追記
133
+ 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'
134
+ [4] pry(main)> User.create(nickname: "hoge", email: "example@fuga.com", password: "123456", password_confirmation: "123456",team_id: "2",favorite_player: "",introduction: "")
135
+ (0.2ms) BEGIN
136
+ User Exists? (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'example@fuga.com' LIMIT 1
137
+ 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')
138
+ (0.9ms) COMMIT
139
+ => #<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">
131
140
 
141
+ [8] pry(main)> Room.create(name: "ルーム1", user_ids:1, content:"")
142
+ User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1
143
+ (0.2ms) BEGIN
144
+ 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')
145
+ 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')
146
+ (0.5ms) COMMIT
147
+ => #<Room:0x00007ff5eb34a650
148
+ id: 9,
149
+ name: "ルーム1",
150
+ content: "",
151
+ created_at: Sun, 30 Aug 2020 11:38:29 JST +09:00,
152
+ updated_at: Sun, 30 Aug 2020 11:38:29 JST +09:00>
153
+ ```
132
154
  ### 試したこと
133
155
 
134
156
  最新の状態で再起動

1

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

2020/08/30 02:53

投稿

HaTea
HaTea

スコア16

title CHANGED
File without changes
body CHANGED
@@ -28,6 +28,7 @@
28
28
  end
29
29
 
30
30
  ```
31
+
31
32
  ```ruby
32
33
  models/user.rb
33
34
 
@@ -57,7 +58,77 @@
57
58
  end
58
59
 
59
60
  ```
61
+ ```html
62
+ index.html.erb ←追記
60
63
 
64
+ <%=form_with model: @room, local: true do |f|%>
65
+ <%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
66
+ <div class='chat-room-form__field--right'>
67
+ <%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
68
+ </div>
69
+ <%= f.label :内容, class: 'chat-room-form__label'%>
70
+ <div class='chat-room-form__field--right'>
71
+ <%= f.text_area :content, class: 'chat__room_name chat-room-form__input', size: "30×30"%>
72
+ </div>
73
+ <%= f.submit class: 'chat-room-form__action-btn'%>
74
+ <%end%>
75
+ ```
76
+ ```ruby
77
+ rooms_controller←追記
78
+
79
+ class RoomsController < ApplicationController
80
+ def index
81
+ @room = Room.new
82
+ @rooms = Room.all.order(id: "DESC")
83
+ end
84
+ def create
85
+ @room = Room.new(room_params)
86
+ if @room.save
87
+ redirect_to root_path
88
+ else
89
+ render :inde
90
+ end
91
+ end
92
+ def destroy
93
+ room = Room.find(params[:id])
94
+ room.destroy
95
+ redirect_to root_path
96
+ end
97
+
98
+ private
99
+ def room_params
100
+ params.require(:room).permit(:name,:content, user_ids:[])
101
+ end
102
+ end
103
+ ```
104
+ ```
105
+ ログ←追記
106
+ Started POST "/rooms" for ::1 at 2020-08-29 17:15:37 +0900
107
+ Processing by RoomsController#create as HTML
108
+ Parameters: {"authenticity_token"=>"MDb1owe+4mdaAauyjwGJquTMrrtKUd1fbinr0e3nbBxD65/ZhUaDJjkSYNV4eMDIWFs3YNqrf1UzKCRqboJZVg==", "room"=>{"name"=>"テストチャットルーム", "content"=>"test"}, "commit"=>"Create Room"}
109
+ (0.1ms) BEGIN
110
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
111
+ 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')
112
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
113
+ (0.3ms) COMMIT
114
+ ↳ app/controllers/rooms_controller.rb:8:in `create'
115
+ Redirected to http://localhost:3000/
116
+ Completed 302 Found in 5ms (ActiveRecord: 0.8ms | Allocations: 3370)
117
+
118
+
119
+ Started GET "/" for ::1 at 2020-08-29 17:15:37 +0900
120
+ Processing by RoomsController#index as HTML
121
+ Rendering rooms/index.html.erb within layouts/application
122
+ Rendered shared/_header.html.erb (Duration: 0.0ms | Allocations: 6)
123
+ User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
124
+ ↳ app/views/rooms/index.html.erb:9
125
+ Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` ORDER BY `rooms`.`id` DESC
126
+ ↳ app/views/rooms/index.html.erb:101
127
+ Rendered rooms/index.html.erb within layouts/application (Duration: 28.1ms | Allocations: 14430)
128
+ [Webpacker] Everything's up-to-date. Nothing to do
129
+ Completed 200 OK in 40ms (Views: 38.6ms | ActiveRecord: 0.6ms | Allocations: 26544)
130
+ ```
131
+
61
132
  ### 試したこと
62
133
 
63
134
  最新の状態で再起動