前提・実現したいこと
ユーザー情報の編集機能を実装しているのですが、updateのところでエラーが出てしまい躓いています。
該当のソースコード
users_controller.rb
ruby
1def show 2 @user = User.find(params[:id]) 3 @currentUserEntry = Entry.where(user_id: current_user.id) 4 @userEntry = Entry.where(user_id: @user.id) 5 6 unless @user.id == current_user.id 7 @currentUserEntry.each do |cu| 8 @userEntry.each do |u| 9 if cu.room_id == u.room_id 10 @isRoom = true 11 @roomId = cu.room_id 12 end 13 end 14 end 15 unless @isRoom 16 @room = Room.new 17 @entry = Entry.new 18 end 19 end 20 end 21 22 def edit 23 24 end 25 26 def update 27 @user.update(user_params) 28 if @user.save 29 redirect_to user_path(@user.id) 30 else 31 render :edit 32 end 33 end 34 35private 36 37 def user_params 38 params.require(:user).permit(:image, :image_cache, :email, :password, :password_confirmation, :nickname, :comment, :content, :twitter, :instagram).merge(user_id: current_user.id) 39 end
application_controller.rb
ruby
1before_action :configure_permitted_parameters, if: :devise_controller? 2 before_action :basic_auth 3 4 private 5 6 def configure_permitted_parameters 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :comment, :content, :twitter, :instagram, :image, :image_cache]) 8 devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :comment, :content, :twitter, :instagram, :image, :image_cache]) 9 end
registrations_controller.rb
ruby
1protected 2 3 def update_resource(resource, params) 4 resource.update_without_current_password(params) 5 resource.image.attach(account_update_params[:image]) 6 end 7 8 def after_update_path_for(resource) 9 user_path(@user.id) 10 end
edit.html.erb
ruby
1<%= form_with model: @user, url: edit_user_registration_path, class: 'registration-main', local: true do |f| %> 2 3<div class='form-wrap'> 4 <div class='form-header'> 5 <h1 class='form-header-text'> 6 ユーザー情報入力 7 </h1> 8 </div> 9 10 <%= render 'shared/error_messages', model: f.object %> 11 12 <div class="form-group"> 13 <div class='form-text-wrap'> 14 <label class="form-text">ニックネーム</label> 15 <span class="indispensable">必須</span> 16 </div> 17 <%= f.text_area :nickname, class:"input-default", id:"nickname", maxlength:"40" %> 18 </div> 19 <div class="form-group"> 20 <div class='form-text-wrap'> 21 <label class="form-text">メールアドレス</label> 22 <span class="indispensable">必須</span> 23 </div> 24 <%= f.email_field :email, class:"input-default", id:"email", placeholder:"PC・携帯どちらでも可", autofocus: true %> 25 </div> 26 <div class="form-group"> 27 <div class='form-text-wrap'> 28 <label class="form-text">パスワード</label> 29 <span class="indispensable">必須</span> 30 </div> 31 <%= f.password_field :password, class:"input-default", id:"password", placeholder:"6文字以上の半角英数字" %> 32 <p class='info-text'>※英字と数字の両方を含めて設定してください</p> 33 </div> 34 <div class="form-group"> 35 <div class='form-text-wrap'> 36 <label class="form-text">パスワード(確認)</label> 37 <span class="indispensable">必須</span> 38 </div> 39 <%= f.password_field :password_confirmation, class:"input-default", id:"password-confirmation", placeholder:"同じパスワードを入力して下さい" %> 40 </div> 41 <div class="img-upload"> 42 <div class="click-upload"> 43 <p> 44 クリックして画像をアップロード 45 </p> 46 <%= f.file_field :image, id:"item-image" %> 47 <%= f.hidden_field :image_cache %> 48 </div> 49 </div> 50 <div class="form-group"> 51 <div class='form-text-wrap'> 52 <label class="form-text">固定コメント</label> 53 </div> 54 <%= f.text_area :comment, class:"input-default", id:"comment", maxlength:"40" %> 55 </div> 56 <div class="form-group"> 57 <div class='form-text-wrap'> 58 <label class="form-text">自由記載欄</label> 59 </div> 60 <%= f.text_field :content, class:"input-default", id:"content", placeholder:"コメント・自己紹介など" %> 61 </div> 62 <div class="form-group"> 63 <div class='form-text-wrap'> 64 <label class="form-text">SNSアカウント登録</label> 65 </div> 66 <%= image_tag "twitter.png", class: :twitter_icon %> 67 <%= f.text_field :twitter, class:"input-default", id:"twitter", placeholder:"URLを入力してください" %> 68 69 <%= image_tag "instagram.png", class: :instagram_icon %> 70 <%= f.text_field :instagram, class:"input-default", id:"instagram", placeholder:"URLを入力してください" %> 71 </div> 72 <div class='register-btn'> 73 <%= f.submit "登録" ,class:"register-red-btn" %> 74 </div> 75</div> 76<% end %>
schema.rb
ruby
1ActiveRecord::Schema.define(version: 2022_04_25_062328) do 2 3 create_table "active_storage_attachments", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.string "name", null: false 5 t.string "record_type", null: false 6 t.bigint "record_id", null: false 7 t.bigint "blob_id", null: false 8 t.datetime "created_at", null: false 9 t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" 10 t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true 11 end 12 13 create_table "active_storage_blobs", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 14 t.string "key", null: false 15 t.string "filename", null: false 16 t.string "content_type" 17 t.text "metadata" 18 t.bigint "byte_size", null: false 19 t.string "checksum", null: false 20 t.datetime "created_at", null: false 21 t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true 22 end 23 24 create_table "entries", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 25 t.bigint "user_id", null: false 26 t.bigint "room_id", null: false 27 t.datetime "created_at", precision: 6, null: false 28 t.datetime "updated_at", precision: 6, null: false 29 t.index ["room_id"], name: "index_entries_on_room_id" 30 t.index ["user_id"], name: "index_entries_on_user_id" 31 end 32 33 create_table "messages", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 34 t.text "message", null: false 35 t.bigint "user_id", null: false 36 t.bigint "room_id", null: false 37 t.datetime "created_at", precision: 6, null: false 38 t.datetime "updated_at", precision: 6, null: false 39 t.index ["room_id"], name: "index_messages_on_room_id" 40 t.index ["user_id"], name: "index_messages_on_user_id" 41 end 42 43 create_table "rooms", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 44 t.bigint "user_id", null: false 45 t.datetime "created_at", precision: 6, null: false 46 t.datetime "updated_at", precision: 6, null: false 47 t.index ["user_id"], name: "index_rooms_on_user_id" 48 end 49 50 create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 51 t.string "email", default: "", null: false 52 t.string "encrypted_password", default: "", null: false 53 t.string "image" 54 t.string "image_cache" 55 t.string "nickname", null: false 56 t.string "comment" 57 t.text "content" 58 t.string "twitter" 59 t.string "instagram" 60 t.string "reset_password_token" 61 t.datetime "reset_password_sent_at" 62 t.datetime "remember_created_at" 63 t.datetime "created_at", precision: 6, null: false 64 t.datetime "updated_at", precision: 6, null: false 65 t.index ["email"], name: "index_users_on_email", unique: true 66 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 67 end 68 69 add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" 70 add_foreign_key "entries", "rooms" 71 add_foreign_key "entries", "users" 72 add_foreign_key "messages", "rooms" 73 add_foreign_key "messages", "users" 74 add_foreign_key "rooms", "users" 75end 76
routes.rb
ruby
1Rails.application.routes.draw do 2 devise_for :users, controllers: { registrations: 'users/registrations' } 3 root to: "users#index" 4 resources :users 5 resources :messages, only: [:create] 6 resources :rooms, only: [:create, :show] 7end
自分で調べたことや試したこと
undefined method `update' for nil:NilClassということで、updateする対象の@userが空になっていると思い、まずパラメーターを確認しました。パラメーターの情報は送れていましたので、変数の定義に問題があると思い、users_controllerのupdateの中の@userをcurrent_userにしたところ、unknown attribute 'user_id' for User.という別のエラーが発生してしまい、原因が分からなくなってしまいましたので、お力をお貸しいただければ幸いです。
追記
ご指摘いただいたように、@user= User.find(params[:id])で値を入れたところ、上記のエラーが発生してしまいました。一応before_actionで括ってみたのですが、再度同じエラーが出ましたので、どなたかご教授いただければ幸いです。
使っているツールのバージョンなど補足情報
Rails 6.0.0
Ruby 2.6.5

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/04/27 01:34
退会済みユーザー
2022/04/27 02:25
2022/04/27 11:27
退会済みユーザー
2022/04/28 00:53
退会済みユーザー
2022/04/28 01:06 編集
2022/04/28 01:53
退会済みユーザー
2022/04/28 03:05
2022/04/28 04:04
退会済みユーザー
2022/04/28 04:31
退会済みユーザー
2022/04/28 04:31
2022/04/28 04:40
退会済みユーザー
2022/04/28 04:45
退会済みユーザー
2022/04/28 04:49
2022/04/28 07:06
退会済みユーザー
2022/04/28 07:12