質問編集履歴

3

---追記---

2018/12/13 03:43

投稿

taishiaaaaa
taishiaaaaa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -131,3 +131,247 @@
131
131
  と表示されました。
132
132
 
133
133
  idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。
134
+
135
+
136
+
137
+ <-------------------------------------------追記------------------------------------------>
138
+
139
+
140
+
141
+ invite.rb
142
+
143
+ ```invite.rb
144
+
145
+ class Invite < ApplicationRecord
146
+
147
+ has_many :messages, foreign_key: "fromid"
148
+
149
+ belongs_to :users
150
+
151
+ end
152
+
153
+
154
+
155
+ ```
156
+
157
+
158
+
159
+ user.rb
160
+
161
+ ```ここに言語を入力
162
+
163
+ class User < ApplicationRecord
164
+
165
+ # Include default devise modules. Others available are:
166
+
167
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
168
+
169
+
170
+
171
+ has_many :messages, foreign_key: "toid"
172
+
173
+ has_many :invites, foreign_key: "fromid"
174
+
175
+
176
+
177
+ devise :database_authenticatable, :registerable,
178
+
179
+ :recoverable, :rememberable, :trackable, :validatable,
180
+
181
+ :lockable, :timeoutable, :omniauthable
182
+
183
+ def self.find_for_oauth(auth)
184
+
185
+ user = User.where(uid: auth.uid, provider: auth.provider).first
186
+
187
+
188
+
189
+ unless user
190
+
191
+ user = User.create(
192
+
193
+ uid: auth.uid,
194
+
195
+ provider: auth.provider,
196
+
197
+ email: User.dummy_email(auth),
198
+
199
+ username: auth.info.nickname,
200
+
201
+ password: Devise.friendly_token[0, 20],
202
+
203
+ image: auth.info.image
204
+
205
+ )
206
+
207
+ end
208
+
209
+
210
+
211
+ user
212
+
213
+
214
+
215
+ end
216
+
217
+
218
+
219
+ def to_param
220
+
221
+ username
222
+
223
+ end
224
+
225
+
226
+
227
+
228
+
229
+ private
230
+
231
+
232
+
233
+ def self.dummy_email(auth)
234
+
235
+ "#{auth.uid}-#{auth.provider}@example.com"
236
+
237
+ end
238
+
239
+ end
240
+
241
+
242
+
243
+ ```
244
+
245
+
246
+
247
+ schema.rb
248
+
249
+ ```ここに言語を入力
250
+
251
+ # This file is auto-generated from the current state of the database. Instead
252
+
253
+ # of editing this file, please use the migrations feature of Active Record to
254
+
255
+ # incrementally modify your database, and then regenerate this schema definition.
256
+
257
+ #
258
+
259
+ # Note that this schema.rb definition is the authoritative source for your
260
+
261
+ # database schema. If you need to create the application database on another
262
+
263
+ # system, you should be using db:schema:load, not running all the migrations
264
+
265
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
266
+
267
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
268
+
269
+ #
270
+
271
+ # It's strongly recommended that you check this file into your version control system.
272
+
273
+
274
+
275
+ ActiveRecord::Schema.define(version: 2018_12_06_044716) do
276
+
277
+
278
+
279
+ create_table "invites", force: :cascade do |t|
280
+
281
+ t.integer "fromid"
282
+
283
+ t.text "content"
284
+
285
+ t.text "title"
286
+
287
+ t.datetime "created_at", null: false
288
+
289
+ t.datetime "updated_at", null: false
290
+
291
+ end
292
+
293
+
294
+
295
+ create_table "messages", force: :cascade do |t|
296
+
297
+ t.integer "fromid"
298
+
299
+ t.integer "toid"
300
+
301
+ t.text "content"
302
+
303
+ t.text "title"
304
+
305
+ t.datetime "created_at", null: false
306
+
307
+ t.datetime "updated_at", null: false
308
+
309
+ end
310
+
311
+
312
+
313
+ create_table "users", force: :cascade do |t|
314
+
315
+ t.string "email", default: "", null: false
316
+
317
+ t.string "encrypted_password", default: "", null: false
318
+
319
+ t.string "reset_password_token"
320
+
321
+ t.datetime "reset_password_sent_at"
322
+
323
+ t.datetime "remember_created_at"
324
+
325
+ t.integer "sign_in_count", default: 0, null: false
326
+
327
+ t.datetime "current_sign_in_at"
328
+
329
+ t.datetime "last_sign_in_at"
330
+
331
+ t.string "current_sign_in_ip"
332
+
333
+ t.string "last_sign_in_ip"
334
+
335
+ t.string "confirmation_token"
336
+
337
+ t.datetime "confirmed_at"
338
+
339
+ t.datetime "confirmation_sent_at"
340
+
341
+ t.string "unconfirmed_email"
342
+
343
+ t.integer "failed_attempts", default: 0, null: false
344
+
345
+ t.string "unlock_token"
346
+
347
+ t.datetime "locked_at"
348
+
349
+ t.datetime "created_at", null: false
350
+
351
+ t.datetime "updated_at", null: false
352
+
353
+ t.string "provider"
354
+
355
+ t.string "uid"
356
+
357
+ t.string "username"
358
+
359
+ t.string "image"
360
+
361
+ t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
362
+
363
+ t.index ["email"], name: "index_users_on_email", unique: true
364
+
365
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
366
+
367
+ t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
368
+
369
+ end
370
+
371
+
372
+
373
+ end
374
+
375
+
376
+
377
+ ```

2

エラー文の追加

2018/12/13 03:43

投稿

taishiaaaaa
taishiaaaaa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -46,11 +46,11 @@
46
46
 
47
47
  def create
48
48
 
49
- @user = User.find(params[:user_id])
49
+ @invite = Invite.new(fromid: params[:user_id], content: params[:content], title: params[:title])
50
50
 
51
- @invite = Invite.create(fromid: params[:user_id], content: params[:content], title: params[:title])
51
+ @invite.save
52
52
 
53
- redirect_to user_invite_path(user_username: @user.username, id: @invite.id)
53
+ redirect_to user_invite_path(id: @invite.id)
54
54
 
55
55
  end
56
56
 
@@ -95,3 +95,39 @@
95
95
  <% end %>
96
96
 
97
97
  ```
98
+
99
+
100
+
101
+
102
+
103
+ submitを実行すると
104
+
105
+
106
+
107
+ ActionController::UrlGenerationError in InvitesController#create
108
+
109
+
110
+
111
+ No route matches {:action=>"show", :controller=>"invites", :id=>nil, :user_username=>"tarou"}, possible unmatched constraints: [:id]
112
+
113
+
114
+
115
+ parameters
116
+
117
+ {"utf8"=>"✓",
118
+
119
+ "authenticity_token"=>"*********************",
120
+
121
+ "invite"=>{"title"=>"タイトル", "content"=>"コンテンツ"},
122
+
123
+ "user_id"=>"4",
124
+
125
+ "commit"=>"投稿する",
126
+
127
+ "user_username"=>"tarou"}
128
+
129
+
130
+
131
+ と表示されました。
132
+
133
+ idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。

1

controllerにある余分なコードを修正

2018/12/11 04:53

投稿

taishiaaaaa
taishiaaaaa

スコア30

test CHANGED
File without changes
test CHANGED
@@ -80,10 +80,6 @@
80
80
 
81
81
  <%= f.text_field :title %>
82
82
 
83
-
84
-
85
- <%= params[:user_username] %>
86
-
87
83
 
88
84
 
89
85
  <%= f.label :content %>