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

質問編集履歴

3

---追記---

2018/12/13 03:43

投稿

taishiaaaaa
taishiaaaaa

スコア30

title CHANGED
File without changes
body CHANGED
@@ -64,4 +64,126 @@
64
64
  "user_username"=>"tarou"}
65
65
 
66
66
  と表示されました。
67
- idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。
67
+ idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。
68
+
69
+ <-------------------------------------------追記------------------------------------------>
70
+
71
+ invite.rb
72
+ ```invite.rb
73
+ class Invite < ApplicationRecord
74
+ has_many :messages, foreign_key: "fromid"
75
+ belongs_to :users
76
+ end
77
+
78
+ ```
79
+
80
+ user.rb
81
+ ```ここに言語を入力
82
+ class User < ApplicationRecord
83
+ # Include default devise modules. Others available are:
84
+ # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
85
+
86
+ has_many :messages, foreign_key: "toid"
87
+ has_many :invites, foreign_key: "fromid"
88
+
89
+ devise :database_authenticatable, :registerable,
90
+ :recoverable, :rememberable, :trackable, :validatable,
91
+ :lockable, :timeoutable, :omniauthable
92
+ def self.find_for_oauth(auth)
93
+ user = User.where(uid: auth.uid, provider: auth.provider).first
94
+
95
+ unless user
96
+ user = User.create(
97
+ uid: auth.uid,
98
+ provider: auth.provider,
99
+ email: User.dummy_email(auth),
100
+ username: auth.info.nickname,
101
+ password: Devise.friendly_token[0, 20],
102
+ image: auth.info.image
103
+ )
104
+ end
105
+
106
+ user
107
+
108
+ end
109
+
110
+ def to_param
111
+ username
112
+ end
113
+
114
+
115
+ private
116
+
117
+ def self.dummy_email(auth)
118
+ "#{auth.uid}-#{auth.provider}@example.com"
119
+ end
120
+ end
121
+
122
+ ```
123
+
124
+ schema.rb
125
+ ```ここに言語を入力
126
+ # This file is auto-generated from the current state of the database. Instead
127
+ # of editing this file, please use the migrations feature of Active Record to
128
+ # incrementally modify your database, and then regenerate this schema definition.
129
+ #
130
+ # Note that this schema.rb definition is the authoritative source for your
131
+ # database schema. If you need to create the application database on another
132
+ # system, you should be using db:schema:load, not running all the migrations
133
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
134
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
135
+ #
136
+ # It's strongly recommended that you check this file into your version control system.
137
+
138
+ ActiveRecord::Schema.define(version: 2018_12_06_044716) do
139
+
140
+ create_table "invites", force: :cascade do |t|
141
+ t.integer "fromid"
142
+ t.text "content"
143
+ t.text "title"
144
+ t.datetime "created_at", null: false
145
+ t.datetime "updated_at", null: false
146
+ end
147
+
148
+ create_table "messages", force: :cascade do |t|
149
+ t.integer "fromid"
150
+ t.integer "toid"
151
+ t.text "content"
152
+ t.text "title"
153
+ t.datetime "created_at", null: false
154
+ t.datetime "updated_at", null: false
155
+ end
156
+
157
+ create_table "users", force: :cascade do |t|
158
+ t.string "email", default: "", null: false
159
+ t.string "encrypted_password", default: "", null: false
160
+ t.string "reset_password_token"
161
+ t.datetime "reset_password_sent_at"
162
+ t.datetime "remember_created_at"
163
+ t.integer "sign_in_count", default: 0, null: false
164
+ t.datetime "current_sign_in_at"
165
+ t.datetime "last_sign_in_at"
166
+ t.string "current_sign_in_ip"
167
+ t.string "last_sign_in_ip"
168
+ t.string "confirmation_token"
169
+ t.datetime "confirmed_at"
170
+ t.datetime "confirmation_sent_at"
171
+ t.string "unconfirmed_email"
172
+ t.integer "failed_attempts", default: 0, null: false
173
+ t.string "unlock_token"
174
+ t.datetime "locked_at"
175
+ t.datetime "created_at", null: false
176
+ t.datetime "updated_at", null: false
177
+ t.string "provider"
178
+ t.string "uid"
179
+ t.string "username"
180
+ t.string "image"
181
+ t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
182
+ t.index ["email"], name: "index_users_on_email", unique: true
183
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
184
+ t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
185
+ end
186
+
187
+ end
188
+
189
+ ```

2

エラー文の追加

2018/12/13 03:43

投稿

taishiaaaaa
taishiaaaaa

スコア30

title CHANGED
File without changes
body CHANGED
@@ -22,9 +22,9 @@
22
22
  end
23
23
 
24
24
  def create
25
- @user = User.find(params[:user_id])
26
- @invite = Invite.create(fromid: params[:user_id], content: params[:content], title: params[:title])
25
+ @invite = Invite.new(fromid: params[:user_id], content: params[:content], title: params[:title])
26
+ @invite.save
27
- redirect_to user_invite_path(user_username: @user.username, id: @invite.id)
27
+ redirect_to user_invite_path(id: @invite.id)
28
28
  end
29
29
 
30
30
  def show
@@ -46,4 +46,22 @@
46
46
  <%= hidden_field_tag :user_id, @user.id %>
47
47
  <%= f.submit "投稿する"%>
48
48
  <% end %>
49
- ```
49
+ ```
50
+
51
+
52
+ submitを実行すると
53
+
54
+ ActionController::UrlGenerationError in InvitesController#create
55
+
56
+ No route matches {:action=>"show", :controller=>"invites", :id=>nil, :user_username=>"tarou"}, possible unmatched constraints: [:id]
57
+
58
+ parameters
59
+ {"utf8"=>"✓",
60
+ "authenticity_token"=>"*********************",
61
+ "invite"=>{"title"=>"タイトル", "content"=>"コンテンツ"},
62
+ "user_id"=>"4",
63
+ "commit"=>"投稿する",
64
+ "user_username"=>"tarou"}
65
+
66
+ と表示されました。
67
+ idが付与されていないのは恐らく@inviteが保存されていないことが原因だと思われます。

1

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

2018/12/11 04:53

投稿

taishiaaaaa
taishiaaaaa

スコア30

title CHANGED
File without changes
body CHANGED
@@ -39,8 +39,6 @@
39
39
  <%= form_for([@user, @invite]) do |f| %>
40
40
  <%= f.label :title %>
41
41
  <%= f.text_field :title %>
42
-
43
- <%= params[:user_username] %>
44
42
 
45
43
  <%= f.label :content %>
46
44
  <%= f.text_field :content %>