質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.49%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

1回答

1540閲覧

NoMethodError in〜 undefined method に意図しないpathが記載されている。

hk1207

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/01/15 05:36

編集2021/01/16 14:51

前提・実現したいこと

Ruby on Railsを用いて投稿機能をもつWebアプリケーションを作成しております。
前提として、親(投稿内容)に子(詳しい情報)を1対1のネスト関係になります。

親の投稿内容に対して、子(詳しい情報)をネスト関係で紐づけて入力・保存したいのですが、子(詳しい情報)を入力するNewアクションへ遷移する際に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

NoMethodError in Informations#new

Showing /Users/###/projects/###/app/views/informations/new.html.erb where line #1 raised:

undefined method `breed_information_index_path' for #<#Class:0x00007fa31f5d8170:0x00007fa3070733a0>
Did you mean? breed_information_path
breed_informations_path

該当のソースコード

####new.html.erb/inforamtions

ruby

1<%= form_with model: [@breed,@information], local: true do |f| %> 2 3 <div> 4 タイトル 5 </div> 6 <%= f.text_field :breedname %> 7 8 <div> 9 産地 10 </div> 11 <%= f.text_field :locality %> 12 13 <div> 14 累代 15 </div> 16 <%= f.collection_select(:generation_id, Generation.all, :id, :name, {}, {class:"select-box", id:"generation"}) %> 17 18 <div> 19 エサ 20 </div> 21 <%= f.collection_select(:food_id, Food.all, :id, :name, {}, {class:"select-box", id:"food"}) %> 22 23 <div> 24 メモ 25 </div> 26 <%= f.text_area :memo %> 27 28 <div> 29 <%= f.submit "入力する"%> 30 </div> 31<% end %> 32

breeds_controller.rb

ruby

1class BreedsController < ApplicationController 2 3 def index 4 end 5 6 def new 7 @breed = Breed.new 8 end 9 10 def create 11 @breed = Breed.new(breed_params) 12 if @breed.save 13 redirect_to root_path 14 else 15 render :new 16 end 17 end 18 19 def show 20 @breed = Breed.find(params[:id]) 21 end 22 23 private 24 def breed_params 25 params.require(:breed).permit(:title, :category_id, :type_name, :date, :result, :note, :image).merge(user_id: current_user.id) 26 end 27 28end

informations_controller.rb

ruby

1def new 2 @information = Information.new 3 end 4 5 def create 6 @information = Information.new(information_params) 7 if @information.save 8 redirect_to root_path 9 else 10 render :new 11 end 12 end 13 14 def show 15 @breed = Breed.find(params[:id]) 16 @information = Information.find(params[:breed_id]) 17 end 18 19 private 20 def information_params 21 params.require(:information).permit(:breedname, :locality, :generation_id, :food_id, :memo).merge(breed_id: params[:breed_id]) 22 end 23end 24

ルーティング

ruby

1 Prefix Verb URI Pattern Controller#Action 2 root GET / breeds#index 3 new_user_session GET /users/sign_in(.:format) devise/sessions#new 4 user_session POST /users/sign_in(.:format) devise/sessions#create 5 destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy 6 new_user_password GET /users/password/new(.:format) devise/passwords#new 7 edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 8 user_password PATCH /users/password(.:format) devise/passwords#update 9 PUT /users/password(.:format) devise/passwords#update 10 POST /users/password(.:format) devise/passwords#create 11 cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel 12 new_user_registration GET /users/sign_up(.:format) devise/registrations#new 13 edit_user_registration GET /users/edit(.:format) devise/registrations#edit 14 user_registration PATCH /users(.:format) devise/registrations#update 15 PUT /users(.:format) devise/registrations#update 16 DELETE /users(.:format) devise/registrations#destroy 17 POST /users(.:format) devise/registrations#create 18 user GET /users/:id(.:format) users#show 19 breed_information_more_informations POST /breeds/:breed_id/informations/:information_id/more_informations(.:format) more_informations#create 20new_breed_information_more_information GET /breeds/:breed_id/informations/:information_id/more_informations/new(.:format) more_informations#new 21 breed_information_more_information GET /breeds/:breed_id/informations/:information_id/more_informations/:id(.:format) more_informations#show 22 breed_informations POST /breeds/:breed_id/informations(.:format) informations#create 23 new_breed_information GET /breeds/:breed_id/informations/new(.:format) informations#new 24 breed_information GET /breeds/:breed_id/informations/:id(.:format) informations#show 25 breeds GET /breeds(.:format) breeds#index 26 POST /breeds(.:format) breeds#create 27 new_breed GET /breeds/new(.:format) breeds#new 28 breed GET /breeds/:id(.:format) breeds#show 29

breed.rb

ruby

1class Breed < ApplicationRecord 2 belongs_to :user 3 has_one_attached :image 4 has_one :information 5 6 extend ActiveHash::Associations::ActiveRecordExtensions 7 belongs_to_active_hash :category 8 9 with_options presence: true do 10 validates :title 11 validates :category_id 12 validates :type_name 13 validates :date 14 validates :result 15 validates :note 16 validates :image 17 end 18 19 validates :category_id, numericality: { other_than: 1 } 20end

information.rb

ruby

1class Information < ApplicationRecord 2 belongs_to :breed 3 has_many :more_informations 4 5 extend ActiveHash::Associations::ActiveRecordExtensions 6 belongs_to_active_hash :generation 7 belongs_to_active_hash :food 8 9 with_options presence: true do 10 validates :breedname 11 validates :locality 12 validates :generation_id 13 validates :food_id 14 end 15 16 validates :generation_id, numericality: { other_than: 1 } 17 validates :food_id, numericality: { other_than: 1 } 18end

routes.rb

Rails.application.routes.draw do root to:"breeds#index" devise_for :users resources :users, only: :show resources :breeds, only: [:index, :new, :create, :show] do resources :informations, only: [:new, :create, :show] do resources :more_informations, only: [:new, :create, :show] end end end

修正後のbreeds_controller.rb

ruby

1class InformationsController < ApplicationController 2 3 4 def new 5 # binding.pry 6 @breed = Breed.find(params[:breed_id]) 7 @information = @breed.build_information 8 # @information = Information.new 9 end 10 11 def create 12 @information = Information.new(information_params) 13 if @information.save 14 redirect_to root_path 15 else 16 render :new 17 end 18 end 19 20 def show 21 @information = Information.find(params[:breed_id]) 22 end 23 24 private 25 def information_params 26 params.require(:information).permit(:breedname, :locality, :generation_id, :food_id, :memo).merge(breed_id: params[:breed_id]) 27 end 28end 29 30 31

試したこと

rails routesでルーティングのパスを確認し、link to でbreed_information_path(@breed.information.id)
を記載し対象ページ遷移しようとしております。
ネストしているform_withの保存する対象のモデル(@breed,@information)を記載し、保存先に誤りがないか確認しております。

またエラー文に記載してあるDid you mean?breed_informations_pathを参考にし、
<%= form_with model: [@breed,@information], url:breed_informations_path,local: true do |f| %>
に訂正すると入力ページに遷移するのですが、
<%= form_with model: @information, url:breed_informations_path,local: true do |f| %>
とこちらの書き方でも入力ページに遷移し、URLを記載するとモデルの記述方法に関係なくページ遷移するので正しい書き方ではないのかと思っております。

エラー分にあるbreed_information_index_pathというpathがそもそもルーティング内で記載されていないのでなぜこのようなpathが出てきたのか原因がわかっておりません。

必要な追加コードがありましたら、記載していただけると助かります。
初めての質問で至らない所が多いかと思いますが宜しくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2021/01/16 08:17

config/routes.rbも載せてください。 breed と information のところだけで良いので
hk1207

2021/01/16 08:35

ご連絡ありがとうございます。 新たにroutes.rbを記載しましたのでご確認お願います。 宜しくお願いいたします。
guest

回答1

0

Information は Breed に belongs_to なので、breedなしではcreate出来ません。
def new @information = Information.new end
の時点で どの breed の Informationなのかは決まっていますか?
いろいろな方法がありますが、たとえば
@breed = そのBreed して
@information = @breed.information.build
してみてください。

@breedがnilなのでおかしなpathになったのでしょう

投稿2021/01/16 08:44

winterboum

総合スコア23324

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hk1207

2021/01/16 09:22

ご連絡ありがとうございます。 ご指摘の通りinformationにnewアクション時にどのbreedかをしていしておりませんでした。 現在外出中なので帰宅次第、検証してみたいと思います。 ありがとうございます。
hk1207

2021/01/16 14:59

ご連絡ありがとうございます。 ご指摘いただいたようにNewアクションの編集を行いましたが同じエラー分が出てしまい解決できませんでした。 def new @breed = Breed.find(params[:breed_id]) @information = @breed.build_information end has oneの関係なのでbuildの記載方法を変え、binding.pryでparamsの確認をし、breed_idがきてることは確認できております。 記載方法を変えながら試しているのですがまだ解決できておりません。 もし宜しければまたご指摘頂いてもよろしいでしょうか?宜しくお願いいたします。
winterboum

2021/01/16 23:44

その params[:breed_id] って、informations_controller#new を呼ぶパラメーターに設定されてます?
hk1207

2021/01/17 04:25

ご連絡有難うございます。 <%= link_to '飼育情報入力', new_breed_information_path(@breed.id) %> 上記の記載方法で、breeds/show.html.erbからInformations_controller#newへ遷移するようにしております。 こちらの方法でbreedのIDに紐付けできているかと認識しているのですが間違っているのでしょうか? 何度も質問して申し訳ありませんがご回答頂けますと幸いです。 よろしくお願いいたします。
winterboum

2021/01/17 06:41

紐ついてますね。 はて。。。。 申し訳ない、力不足の様です
hk1207

2021/01/17 07:05

いえいえ、とんでもありません。 ご連絡いただき誠にありがとうございます。 もう一度再度見直して解決していきたいと思います。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.49%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問