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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1129閲覧

ネストしたルーティングの設定について

ebi---kani

総合スコア3

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/02 09:02

トップページからshowアクションでページ遷移させようとlink_toを使って実装したところエラーが出てしまいます。
”現在のエントリーチーム情報”という箇所からlink_toで遷移させようとしています。

どなたかご教授ください。。。

#エラー文

ActionController::UrlGenerationError in Tournaments#index Showing /Users/ebinumayuuki/projects/fotsal-goal-app/app/views/tournaments/index.html.erb where line #55 raised: No route matches {:action=>"show", :controller=>"entries", :tournament_id=>nil}, missing required keys: [:id], possible unmatched constraints: [:tournament_id] Extracted source (around line #55): 53 54 55 56 57 58 <span class = "team-show"> <%= link_to "現在のエントリーチーム情報",tournament_entry_path(@tournament_id) %> </div> <% end %>

#rails routes

ruby

1 root GET / tournaments#index 2 tournament_entries GET /tournaments/:tournament_id/entries(.:format) entries#index 3 POST /tournaments/:tournament_id/entries(.:format) entries#create 4 new_tournament_entry GET /tournaments/:tournament_id/entries/new(.:format) entries#new 5 tournament_entry GET /tournaments/:tournament_id/entries/:id(.:format) entries#show 6 tournaments GET /tournaments(.:format) tournaments#index 7 POST /tournaments(.:format) tournaments#create 8 new_tournament GET /tournaments/new(.:format) tournaments#new 9 edit_tournament GET /tournaments/:id/edit(.:format) tournaments#edit 10 tournament GET /tournaments/:id(.:format) tournaments#show 11 PATCH /tournaments/:id(.:format) tournaments#update 12 PUT /tournaments/:id(.:format) tournaments#update 13 DELETE /tournaments/:id(.:format) tournaments#destroy

#entry controller

class EntriesController < ApplicationController before_action :authenticate_user! before_action :set_product, only: [:index,:create] def index @entry = Entry.new end def create @entry = Entry.new(entry_params) if @entry.save redirect_to root_path else render :index end end def new @entry = Entry.new end def show @entries = Entry.find(entry_params) end private def entry_params params.require(:entry).permit(:team_name,:team_captain,:phone).merge(user_id:current_user.id,tournament_id:@tournament.id) end def set_product @tournament = Tournament.find(params[:tournament_id]) end end

#tournament controller

class TournamentsController < ApplicationController before_action :set_q, only: [:index, :search] before_action :set_tournament, only: [:show,:edit,:update,:destroy] def index @tournament = Tournament.all end def new @tournament = Tournament.new end def create @tournament = Tournament.new(tournament_params) if @tournament.save redirect_to root_path else render :new end end def show end def edit end def update if @tournament.update(tournament_params) redirect_to tournament_path(@tournament.id) else render :edit end end def destroy @tournament.destroy redirect_to root_path end def search @results = q.result end private def tournament_params params.require(:tournament).permit(:event_month_id,:event_date_id,:event_time_id,:entry_participation_id,:fee,:event_address,:event_description,:event_title).merge(operation_id: current_operation.id) end def set_q @q = Tournament.ransack(params[:q]) end def set_tournament @tournament = Tournament.find(params[:id]) end end

#routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :operations 3 devise_for :users 4 root to: "tournaments#index" 5 resources :tournaments do 6 resources :entries, only:[:index,:create,:new,:show] 7 end 8end

#トップページ コード

上記省略 <span class = "team-show"> <%= link_to "現在のエントリーチーム情報",tournament_entry_path(@tournament_id) %> </div>

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

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

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

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

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

guest

回答1

0

ベストアンサー

tournament_entry_path(@tournament_id) というとtournament_entry GET /tournaments/:tournament_id/entries/:id(.:format) entries#show
になります。
:tournament_id だけではなく entries の id も必要です

投稿2021/04/02 09:12

winterboum

総合スコア23408

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

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

ebi---kani

2021/04/02 09:43 編集

ありがとうございます! 修正の仕方としては <%= link_to "現在のエントリーチーム情報",tournament_entry_path(@tournament.id,@entries.id) %> このように実装してみたのですが、エラーのままになってしまいます。。。
winterboum

2021/04/02 09:53

tournament_entry_path(@tournament,@entries) または tournament_entry_path(tournament_id: @tournament.id,id: entries.id) で試してください
ebi---kani

2021/04/02 10:08

修正してみたのですが、、、 #tournament_entry_path(@tournament,@entries) ``` ActionController::UrlGenerationError in Tournaments#index Showing /Users/ebinumayuuki/projects/fotsal-goal-app/app/views/tournaments/index.html.erb where line #55 raised: No route matches {:action=>"show", :controller=>"entries", :id=>nil, :tournament_id=>"#<Tournament::ActiveRecord_Relation:0x00007f9bb017e5f0>"}, missing required keys: [:id] Extracted source (around line #55): 53 54 55 56 57 58 <span class = "team-show"> <%= link_to "現在のエントリーチーム情報",tournament_entry_path(@tournament,@entries)%> </div> <% end %> ``` #tournament_entry_path(tournament_id: @tournament.id,id: entries.id) ``` NoMethodError in Tournaments#index Showing /Users/ebinumayuuki/projects/fotsal-goal-app/app/views/tournaments/index.html.erb where line #55 raised: undefined method `id' for #<Tournament::ActiveRecord_Relation:0x00007f9be2496670> Did you mean? ids Extracted source (around line #55): 53 54 55 56 57 58 <span class = "team-show"> <%= link_to "現在のエントリーチーム情報",tournament_entry_path(tournament_id: @tournament.id,id: entries.id) %> </div> <% end %> ``` どちらもエラーが表示されてしまします。。。
winterboum

2021/04/02 12:21

よく見ると @entries ??@entries ここでは Entry のデータはどこにあるの? 「上記省略」されてるからわからん
ebi---kani

2021/04/03 02:04

ご教授ありがとうございます! idの付与の仕方を変えたら無事に実装できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問