いつもお世話になっております。今回もよろしくおねがいします。
タイトルの通り、users_controller内のupdateアクションにおいて、updateメソッドの引数が合わず困っています。「引数に代入しているuser_paramsが引数としてカウントされていないのではないか?」と仮説をたてたのですが、この認識で間違いないでしょうか?
また、一応確認のためupdate_attributesメソッドも使ってみましたが結果は同じでした。
お知恵をお貸しください
発生している問題・エラーメッセージ
Started PATCH "/users/32" for 160.15.13.173 at 2019-08-23 01:24:10 +0000 Cannot render console from 160.15.13.173! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"jzaJrmNU2WBiYVTMDt6R/Alfm5qnFh4aATnMU77xX4dHZQR+GIfVqrHz5whFTIh4f3I14jo7CTgsE750Lzl1qw==", "user"=>{"username"=>"yusuke", "email"=>"yusuke@yahoo.com", "password"=>"[FILTERED]"}, "commit"=>"更新する", "id"=>"32"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 32], ["LIMIT", 1]] (0.1ms) begin transaction (0.1ms) rollback transaction Completed 500 Internal Server Error in 79ms (ActiveRecord: 0.4ms) ArgumentError (wrong number of arguments (given 0, expected 1)): app/controllers/users_controller.rb:31:in `update'
該当のソースコード
app/controssler/users_controller.rb(省略なし・全文)
# frozen_string_literal: true class UsersController < ApplicationController before_action :check_user_login?, only: [:show] def new @user=User.new end def create @user=User.new(user_params) if @user.save current_user = @user redirect_to root_path else #パラムスを持つユーザー render new_user_path end end def edit user_find_by_id end def update user_find_by_id binding.pry if @user.update(user_params) redirect_to user_path(id: current_user.id) else render edit_user_path end end def destroy user_find_by_id @user.destroy end def show user_find_by_id @tours = Tour.where(user_id: @user.id) end def followings user_find_by_id @users = @user.followings.page(params[:page]) render 'follow' end def followers user_find_by_id @users = @user.followers.page(params[:page]) render 'follow' end def favorite @tours = current_user.favtours.page(params[:page]) end private def user_find_by_id @user = User.find_by(id: params[:id]) end def user_params params.require(:user).permit(:username,:email, :password, :password_confirmation,:user_image) end end
app/model/user.rb
# frozen_string_literal: true class User < ApplicationRecord has_many :tours, dependent: :destroy has_many :comments, dependent: :destroy has_many :likes, dependent: :destroy has_many :liked_tours, through: :likes, source: :tour has_many :favorites has_many :favtours, through: :favorites, source: :tour has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverse_of_relationships, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverse_of_relationships, source: :user devise :database_authenticatable, :recoverable, :rememberable, # :validatable, :registerable, :omniauthable mount_uploader :userimage, UserimageUploader has_one_attached :user_image def update_without_current_password(params, *options) params.delete(:current_password) result = update_attributes(params, *options) clean_up_passwords result end end
app/views/user/edit.html.haml
%h3 ユーザー編集ページ = simple_form_for(@user) do |f| = f.error_notification .form-inputs = f.input :username, required: true, autofocus: true, input_html: { autocomplete: "username",class:"form-control" } = f.input :email, required: true, autofocus: true, input_html: {class:"form-control" } = f.input :password, hint: "leave it blank if you don't want to change it", required: false, input_html: { autocomplete: "new-password",class:"form-control" } = f.input :user_image, as: :file, input_html: { class:"mb-3" } .form-actions = f.button :submit, "更新する" %p.mt-3 #{link_to "アカウントを削除する","users/#{@user.id}", data: { confirm: "Are you sure?" }, method: :delete} = link_to "キャンセル", root_path
###追記(binding.pryを使った行の可視化、上記のusers_controller.rbにも追記あり)
From: /home/ec2-user/environment/new_app2/app/controllers/users_controller.rb @ line 32 User sController#update: 29: def update 30: user_find_by_id 31: binding.pry#(実際にはこの行は挿入されていないので、↓のif行が31行目になります) => 32: if @user.update(user_params) 33: redirect_to user_path(id: current_user.id) 34: else : この場合のエラーメッセージは ArgumentError (wrong number of arguments (given 0, expected 1)): app/controllers/users_controller.rb:32:in `update' でした
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。