前提・実現したいこと
railsでwebサービスを作っております。
ユーザー編集画面で、form_withにおいてmodelに2つの値を渡したいのですが、エラーが出てしまいうまくいきません。
エラーコード、ソースコードは下述いたします。
どこが間違っているか教えていただきたいです。
程度の低い質問でしたら申し訳ございません。
また、質問に必要な情報等に不足があれば、お手数ですがご指摘いただければ、追記いたします。
よろしくお願いいたします。
該当のソースコード
html
1[edit.html.erb] 2<% provide(:title, 'edit') %> 3 4<div class="edit-wrapper"> 5 <h1 class="edit">Edit</h1> 6 7 <div class="row signup-forms"> 8 <div class="col-md-6 col-md-offset-3"> 9 <%= form_with(model: [@user, @detail], local: true) do |f| %> //該当箇所ここです 10 <%= render 'shared/error_messages'%> 11 12 <%= f.label :name, "Nickname" %> 13 <%= f.text_field :name, class: 'form-control', placeholder: "#{@user.name}" %> 14 15 <%= f.label :image %> 16 <%= f.file_field :image, class: "form-control image-form" %> 17 18 <%= f.label :mother_tongue, "Mother Tongue | 母語" %> 19 <%= f.select :mother_tongue, [["English", "English"], ["Japanese", "Japanese"], 20 ["Other","other"]], include_blank: "Please select.", class:"form-control" %> 21 22 <%= f.label :japanese_level, "Japanese Level | 日本語のレベル" %> 23 <%= f.select :japanese_level, [["Beginner | 初心者", "Beginner | 初心者"], ["Capable of quite basic communication | 簡単な会話ならできる", "Capable of quite basic communication | 簡単な会話ならできる"], 24 ["Capable of daily conversation | 日常会話ができる","Capable of daily conversation | 日常会話ができる"], ["Capable of advanced conversation | 難しい内容の会話ができる","Capable of advanced conversation | 難しい内容の会話ができる"], ["Almost native level | ネイティブとほとんど同レベル","Almost native level | ネイティブとほとんど同レベル"], 25 ["Native | ネイティブ","Native | ネイティブ"]], include_blank: "Please select.", class:"form-control" %> 26 27 <%= f.label :english_level, "English Level | 英語のレベル" %> 28 <%= f.select :english_level, [["Beginner | 初心者", "Beginner | 初心者"], ["Capable of quite basic communication | 簡単な会話ならできる", "Capable of quite basic communication | 簡単な会話ならできる"], 29 ["Capable of daily conversation | 日常会話ができる","Capable of daily conversation | 日常会話ができる"], ["Capable of advanced conversation | 難しい内容の会話ができる","Capable of advanced conversation | 難しい内容の会話ができる"], ["Almost native level | ネイティブとほとんど同レベル","Almost native level | ネイティブとほとんど同レベル"], 30 ["Native | ネイティブ","Native | ネイティブ"]], include_blank: "Please select.", class:"form-control" %> 31 32 <%= f.label :gender, "Gender(optional) | ジェンダー(任意)" %> 33 <%= f.select :gender, [["he/him | 男性", "he/him | 男性"], ["she/her | 女性", "she/her | 女性"], 34 ["they/them | その他","they/them | その他"], ["I don't want to tell | 言いたくない","I don't want to tell | 言いたくない"]], include_blank: "Please select.", class:"form-control" %> 35 36 <%= f.label :region, "Region | 地域" %> 37 <%= f.select :region, [["Asia | アジア", "Asia | アジア"], ["Europe |ヨーロッパ", "Europe | ヨーロッパ"], 38 ["Africa | アフリカ","Africa | アフリカ"], ["North/Central America | 北/中央アメリカ","North/Central America | 北/中央アメリカ"], ["South America | 南アメリカ","South America | 南アメリカ"], 39 ["Oceania | オセアニア","Oceania | オセアニア"], ["Other | その他","Other | その他"]], include_blank: "Please select.", class:"form-control" %> 40 41 <%= f.label :purpose, "Purpose of Learning | 学習目的" %> 42 <%= f.select :purpose, [["Business | 仕事", "Business | 仕事"], ["School Classes | 学校の授業", "School Classes | 学校の授業"], 43 ["Entrance Exam | 受験","Entrance Exam | 受験"], ["Trip | 旅行","Trip | 旅行"], ["Want to talk with Foreingers | 外国の人と話したい","Want to talk with Foreingers | 外国の人と話したい"], 44 ["Qualifying Exam | 資格試験","Qualifying Exam | 資格試験"], ["Hobby | 趣味","Hobby | 趣味"], ["Other | その他", "Other | その他"]], include_blank: "Please select.", class:"form-control" %> 45 46 <%= f.label :self_introduction, "Self Introduction | 自己紹介" %> 47 <%= f.text_area :self_introduction, class: 'form-control self-intro', placeholder: "It is recommended that you write in simple English. 英語で書くことをお勧めします。簡単な文でも問題ありません。" %> 48 49 <%= f.label :sns_1, "URL of Your SNS 1 (optional) | SNSリンク1(任意)" %> 50 <%= f.text_field :sns_1, class: 'form-control' %> 51 52 <%= f.label :sns_2, "URL of Your SNS 2 (optional) | SNSリンク2(任意)" %> 53 <%= f.text_field :sns_2, class: 'form-control' %> 54 55 <%= f.label :sns_3, "URL of Your SNS 3 (optional) | SNSリンク3(任意)" %> 56 <%= f.text_field :sns_3, class: 'form-control' %> 57 58 59 60 <%= f.submit "Update Your Information", class: "btn btn-primary create" %> 61 <% end %> 62 63 <%= link_to "Change Email", "/users/#{@user.id}/edit_email", class: "btn btn-light btn-lg" %> 64 <%= link_to "Change Password", "/users/#{@user.id}/edit_email", class: "btn btn-secondary btn-lg" %> 65 </div> 66 </div> 67</div>
ruby
1[users_controller.rb] 2class UsersController < ApplicationController 3 4 def show 5 @user = User.find(params[:id]) 6 end 7 8 def new 9 @user = User.new 10 end 11 12 def signup_jp 13 @user = User.new 14 end 15 16 def create 17 @user = User.new(user_params) 18 if @user.save 19 log_in @user 20 flash[:success] = "Welcome to INTE! Your account has been creaeted." 21 redirect_to details_path 22 else 23 flash[:danger] = "Invalid user information. Confirm that you filled all boxes except optional." 24 render 'new' 25 end 26 end 27 28 def details 29 end 30 31 def details_create 32 @detail = Detail.new(detail_params) 33 if @detail.save 34 redirect_to "/users/#{current_user.id}" 35 else 36 render 'details' 37 end 38 end 39 40 def edit 41 @user = User.find(params[:id]) 42 @detail = Detail.find_by(user_id: @user.id ) 43 end 44 45 def update //該当箇所ここです 46 @user = User.find(params[:id]) 47 @detail = Detail.find_by(user_id: @user.id) 48 if @user.update(user_params) || @detail.update(detail_params) 49 redirect_to @user 50 else 51 render 'edit' 52 end 53 end 54 55 56 def edit_email 57 @user = User.find(params[:id]) 58 end 59 60 61 def edit_password 62 @user = User.find(params[:id]) 63 end 64 65 66 67 68 69 private 70 71 def user_params 72 params.require(:user).permit(:name, :email, :image, :password, :password_confirmation) 73 end 74 75 def detail_params 76 params.permit(:authenticity_token, :user_id, :mother_tongue, :japanese_level, :english_level, 77 :gender, :region, :purpose, :self_introduction, :sns_1, :sns_2, :sns_3) 78 end 79 80end 81
ruby
1[routes.rb] 2Rails.application.routes.draw do 3 4 get 'sessions/new' 5 root 'static_pages#language' 6 get '/home_jp', to:'static_pages#home_jp' 7 get '/home_en', to:'static_pages#home_en' 8 get '/signup', to:'users#new' 9 get '/signup_jp', to:'users#signup_jp' 10 post '/signup_jp', to:'users#create' 11 get '/details', to:'users#details' 12 post '/details', to:'users#details_create' 13 get '/login', to:'sessions#new' 14 post '/login', to:'sessions#create' 15 delete '/logout', to:'sessions#destroy' 16 get '/users/:id/edit_email', to:'users#edit_email' 17 patch '/users/:id/edit_email', to:'users#update' 18 get '/users/:id/edit_password', to:'users#edit_password' 19 patch '/users/:id/edit_password', to:'users#update' 20 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 21 resources :users 22end 23
###エラーコード
NoMethodError in Users#edit Showing /home/ubuntu/environment/INTE/app/views/users/edit.html.erb where line #8 raised: undefined method `user_detail_path' for #<#<Class:0x00007f31b28afa80>:0x00007f31b225baf8> Did you mean? user_path Extracted source (around line #8): <div class="row signup-forms"> <div class="col-md-6 col-md-offset-3"> <%= form_with(model: [@user, @detail], local: true) do |f| %> //ここが赤く表示されています <%= render 'shared/error_messages'%> <%= f.label :name, "Nickname" %>
補足情報(FW/ツールのバージョンなど)
Rails 6.0.3
Ruby 2.6.3
AWS Cloud9を使用
modelのcodeも。
特に関連の定義部分