質問編集履歴

3

画像の削除

2022/04/30 14:29

投稿

AKIRA0310
AKIRA0310

スコア15

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,5 @@
1
1
  # 前提・実現したいこと
2
2
  ユーザー情報の編集機能を実装しているのですが、updateのところでエラーが出てしまい躓いています。
3
- # 発生している問題・エラーメッセージ
4
- ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-27/c5598326-2217-425a-a51c-449ccb53cc45.png)
5
3
  # 該当のソースコード
6
4
  ### users_controller.rb
7
5
  ```ruby

2

routes.rbとschema.rbを追加

2022/04/28 01:46

投稿

AKIRA0310
AKIRA0310

スコア15

test CHANGED
File without changes
test CHANGED
@@ -149,6 +149,95 @@
149
149
  </div>
150
150
  <% end %>
151
151
  ```
152
+ ### schema.rb
153
+ ```ruby
154
+ ActiveRecord::Schema.define(version: 2022_04_25_062328) do
155
+
156
+ create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
157
+ t.string "name", null: false
158
+ t.string "record_type", null: false
159
+ t.bigint "record_id", null: false
160
+ t.bigint "blob_id", null: false
161
+ t.datetime "created_at", null: false
162
+ t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
163
+ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
164
+ end
165
+
166
+ create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
167
+ t.string "key", null: false
168
+ t.string "filename", null: false
169
+ t.string "content_type"
170
+ t.text "metadata"
171
+ t.bigint "byte_size", null: false
172
+ t.string "checksum", null: false
173
+ t.datetime "created_at", null: false
174
+ t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
175
+ end
176
+
177
+ create_table "entries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
178
+ t.bigint "user_id", null: false
179
+ t.bigint "room_id", null: false
180
+ t.datetime "created_at", precision: 6, null: false
181
+ t.datetime "updated_at", precision: 6, null: false
182
+ t.index ["room_id"], name: "index_entries_on_room_id"
183
+ t.index ["user_id"], name: "index_entries_on_user_id"
184
+ end
185
+
186
+ create_table "messages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
187
+ t.text "message", null: false
188
+ t.bigint "user_id", null: false
189
+ t.bigint "room_id", null: false
190
+ t.datetime "created_at", precision: 6, null: false
191
+ t.datetime "updated_at", precision: 6, null: false
192
+ t.index ["room_id"], name: "index_messages_on_room_id"
193
+ t.index ["user_id"], name: "index_messages_on_user_id"
194
+ end
195
+
196
+ create_table "rooms", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
197
+ t.bigint "user_id", null: false
198
+ t.datetime "created_at", precision: 6, null: false
199
+ t.datetime "updated_at", precision: 6, null: false
200
+ t.index ["user_id"], name: "index_rooms_on_user_id"
201
+ end
202
+
203
+ create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
204
+ t.string "email", default: "", null: false
205
+ t.string "encrypted_password", default: "", null: false
206
+ t.string "image"
207
+ t.string "image_cache"
208
+ t.string "nickname", null: false
209
+ t.string "comment"
210
+ t.text "content"
211
+ t.string "twitter"
212
+ t.string "instagram"
213
+ t.string "reset_password_token"
214
+ t.datetime "reset_password_sent_at"
215
+ t.datetime "remember_created_at"
216
+ t.datetime "created_at", precision: 6, null: false
217
+ t.datetime "updated_at", precision: 6, null: false
218
+ t.index ["email"], name: "index_users_on_email", unique: true
219
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
220
+ end
221
+
222
+ add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
223
+ add_foreign_key "entries", "rooms"
224
+ add_foreign_key "entries", "users"
225
+ add_foreign_key "messages", "rooms"
226
+ add_foreign_key "messages", "users"
227
+ add_foreign_key "rooms", "users"
228
+ end
229
+
230
+ ```
231
+ ### routes.rb
232
+ ```ruby
233
+ Rails.application.routes.draw do
234
+ devise_for :users, controllers: { registrations: 'users/registrations' }
235
+ root to: "users#index"
236
+ resources :users
237
+ resources :messages, only: [:create]
238
+ resources :rooms, only: [:create, :show]
239
+ end
240
+ ```
152
241
  # 自分で調べたことや試したこと
153
242
  undefined method `update' for nil:NilClassということで、updateする対象の@userが空になっていると思い、まずパラメーターを確認しました。パラメーターの情報は送れていましたので、変数の定義に問題があると思い、users_controllerのupdateの中の@userをcurrent_userにしたところ、unknown attribute 'user_id' for User.という別のエラーが発生してしまい、原因が分からなくなってしまいましたので、お力をお貸しいただければ幸いです。
154
243
 

1

ご回答いただいた方法を試しましたが上手くいきませんでした

2022/04/27 01:31

投稿

AKIRA0310
AKIRA0310

スコア15

test CHANGED
File without changes
test CHANGED
@@ -151,6 +151,10 @@
151
151
  ```
152
152
  # 自分で調べたことや試したこと
153
153
  undefined method `update' for nil:NilClassということで、updateする対象の@userが空になっていると思い、まずパラメーターを確認しました。パラメーターの情報は送れていましたので、変数の定義に問題があると思い、users_controllerのupdateの中の@userをcurrent_userにしたところ、unknown attribute 'user_id' for User.という別のエラーが発生してしまい、原因が分からなくなってしまいましたので、お力をお貸しいただければ幸いです。
154
+
155
+ # 追記
156
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-04-27/97d9dbc5-b6b4-474f-aee3-743c88805f91.png)
157
+ ご指摘いただいたように、@user= User.find(params[:id])で値を入れたところ、上記のエラーが発生してしまいました。一応before_actionで括ってみたのですが、再度同じエラーが出ましたので、どなたかご教授いただければ幸いです。
154
158
  # 使っているツールのバージョンなど補足情報
155
159
  Rails 6.0.0
156
160
  Ruby 2.6.5