Railsを使い、自作アプリ開発中なのですが、editアクションでつまずき、悩んでおります。
ただただ、呼び出すパラメーターがどこにあるのかわからず苦戦しております。。
ブラウザ上の_form_edit_html_erbでレンダリングされたページより値を変更した内容でsubmitすると、当該エラーが発生します。
尚、エラーメッセージはこの一文のみであり、updateアクションのUser.update(:title, :content, :category_id)を指しています。
エラーメッセージの意味はわかるのですが、期待される引数を書いた部分がよく分からず、ご相談させて頂いた次第です。
何卒、宜しくお願い申し上げます。
users_controller_rb class UsersController < ApplicationController before_action :set_user, only: [:show, :edit, :update, :destroy] ・ ・ ・ # GET /users/1/edit def edit @user = User.find(params[:id]) end ・ ・ ・ # PATCH/PUT /users/1 # PATCH/PUT /users/1.json def update User.update(:title, :content, :category_id) respond_to do |format| if @user.update(user_params) format.html { redirect_to @user, notice: 'User was successfully updated.' } format.json { render :show, status: :ok, location: @user } else format.html { render :edit } format.json { render json: @user.errors, status: :unprocessable_entity } end end end ・ ・ ・ private # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:title, :content, :category_id) end end
_form_edit_html_erb <%= form_with(model: user, local: true) do |form| %> <% if user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2> <ul> <% user.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="form-group"> <%= form.label :title %> <%= form.text_field :title, class:"form-control" %> </div> <div class="form-group"> <%= form.label :content %> <%= form.text_field :content, class:"form-control" %> </div> <div class="form-group"> <%= form.label :category %> <%= select_tag 'users[category_id]', options_from_collection_for_select(@categories, :id, :name), class:"form-control"%> </div> <br> <div class="actions"> <%= form.submit %> </div> <% end %>
> User.update(:title, :content, :category_id)
なんのためにこの文が必要だと思っているのですか?
set_userメソッドが私の想定通りならば、User.update(~)は不要に思えます
コメントありがとうございます。
エラーメッセージにあるように、expected 2となっていますので、User.updateの部分で引数をgiveするものかと思っておりました。
勉強の方向性が間違っているのか、expected 2はどこを指しているのか分からず途方に暮れております。もしよろしければご教示頂けないでしょうか?
エラー表記どおり、間違いがあるのは確かに User.update(:title, :content, :category_id) です。
繰り返しますが、何のためにあなたはUser.update(:title, :content, :category_id)を書いているのですか?
私にはエラーを修正した場合と、その1行を消した場合で動作に違いがないように思います。
ご指摘頂いた通り、User.updateの引数は不要と考え、削除しましたが記載を直しても、根本修正をしていないのでエラーのままです。(wrong numberの数は変わりましたが)
伺いたいのは、引数のみが不要ということでしょうか?もしくは、その行自体が不要ということでしょうか?
User.updateの行は必要と思い込んでおりましたので、誤りでしたらご容赦ください。
「その1行を消した場合で動作に違いがない」と既に説明した通り、User.updateの行自体が要らないように
私には見えます。
ご回答頂きありがとうございます。まだお伺いしたいことがありましたが、改めて別チケットで進めることtにいたします。
色々とご教示いただきありがとうございました。
あなたの回答
tips
プレビュー