###前提・実現したい事。
投稿にコメント機能を実装しているのですが、form_withでモデルを複数指定するとエラーが出ます。
###発生している問題・エラーメッセージ
ActionView::Template::Error (undefined method `answer_photo_path' for #<#<Class:0x00007fa1981102b0>:0x00007fa1980b71d8> Did you mean? new_photo_path):
###該当のソースコード
routes.rb
rails
1Rails.application.routes.draw do 2 resources :photos 3 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 4 root to: 'toppages#index' 5 6 get 'login', to: 'sessions#new' 7 post 'login', to: 'sessions#create' 8 delete 'logout', to: 'sessions#destroy' 9 10 get 'signup', to: 'users#new' 11 resources :users, only: [:index, :show, :create] 12 resources :photos 13 resources :answers, only: [:create] 14end 15
photos_controller.rb
rails
1class PhotosController < ApplicationController 2 before_action :set_photo, only: %i[ show edit update destroy ] 3 4 # GET /photos or /photos.json 5 def index 6 @photos = Photo.all 7 end 8 9 # GET /photos/1 or /photos/1.json 10 def show 11 @answer = Answer.new 12 @photo = Photo.find(params[:id]) 13 @answers = @photo.answers 14 end 15 16 # GET /photos/new 17 def new 18 @photo = Photo.new 19 end 20 21 # GET /photos/1/edit 22 def edit 23 end 24 25 # POST /photos or /photos.json 26 def create 27 @photo = current_user.photos.new(photo_params) 28 29 if @photo.save 30 flash[:success] = '投稿に成功しました。' 31 redirect_to photos_path 32 else 33 flash.now[:danger] = "投稿に失敗しました。" 34 render :new 35 end 36 end 37 38 # PATCH/PUT /photos/1 or /photos/1.json 39 def update 40 respond_to do |format| 41 if @photo.update(photo_params) 42 format.html { redirect_to @photo, notice: "Photo was successfully updated." } 43 format.json { render :show, status: :ok, location: @photo } 44 else 45 format.html { render :edit, status: :unprocessable_entity } 46 format.json { render json: @photo.errors, status: :unprocessable_entity } 47 end 48 end 49 end 50 51 # DELETE /photos/1 or /photos/1.json 52 def destroy 53 @photo.destroy 54 respond_to do |format| 55 format.html { redirect_to photos_url, notice: "Photo was successfully destroyed." } 56 format.json { head :no_content } 57 end 58 end 59 60 private 61 # Use callbacks to share common setup or constraints between actions. 62 def set_photo 63 @photo = Photo.find(params[:id]) 64 end 65 66 # Only allow a list of trusted parameters through. 67 def photo_params 68 params.require(:photo).permit(:image, :content) 69 end 70end 71
answers_controller.rb
rails
1class AnswersController < ApplicationController 2 3 def create 4 @photo = Photo.find(params[:photo_id]) 5 @answer = @photo.answer.new(answer_params) 6 if @answer.save 7 redirect_to photo_path(@photo) 8 else 9 flash.now[:alert] = 'コメント入力してください。' 10 end 11 end 12 13 private 14 15 def answer_params 16 params.require(:answer).permit(:content, :photo_id) 17 end 18end 19
photos/show.html.erb
rails
1<p id="notice"><%= notice %></p> 2<div class="row"> 3 <div class="offset-md-3 col-md-5"> 4 <p> 5 6 <%= image_tag @photo.image.url if @photo.image? %> 7 </p> 8 9 <p> 10 <strong>クイズの問題:</strong> 11 <%= @photo.content %> 12 </p> 13 <p> 14 <%= form_with(model:[@answer, @photo], method: :post) do |f| %> 15 <div class="form-group"> 16 <%= f.text_area :content, class: 'form-control', rows: 5 %> 17 </div> 18 <%= f.submit 'Post', class: 'btn btn-primary btn-block' %> 19 <% end %> 20 </p> 21 </div> 22</div> 23 24<%= link_to '戻る', photos_path %> | 25<%= link_to '編集', edit_photo_path(@photo) %> 26
###補足
たりないコードがあれば載せます。
ご教授お願いします。
ruby 2.5.3
rails 5.2.5
あなたの回答
tips
プレビュー