0

「参加者の作成」ボタンをクリックすると、奇妙なルーティング エラーが発生します。明らかに、ルーティングがどのように機能するかについて、私は何かを誤解しています。明確にしていただければ幸いです。

No route matches {:action=>"present_survey", :controller=>"rounds", :program_id=>#
<Program id: 1, name: "JBS 2012", description: "Summer of 2012", open: false, 
locked: true, suppress_hidden_participants: false, created_at: "2012-11-19 22:35:06", 
updated_at: "2012-11-19 22:35:06">, :participant_id=>nil, :round_id=>#<Round id: 9, 
program_id: 1, number: 8, start: 86, fin: 95, status: nil, open: true, open_date: nil, 
created_at: "2012-11-19 22:35:07", updated_at: "2012-11-19 22:35:07">}

関連する routes.rb 行は次のとおりです。

new_program_participant GET /programs/:program_id/participants/new(.:format)  participants#new

関連するコントローラー行は次のとおりです。

class ParticipantsController < ApplicationController
  respond_to :html
  def index
    @program_id = params[:program_id]
    @program = Program.find(@program_id)
    @participants = @program.participants.paginate(page: params[:page])
    respond_with @participants do |format|
      format.html {
        render layout: 'layouts/progtabs'
      }
    end
  end

関連するビュー行は次のとおりです。

<%= link_to "Create a new Participant", new_program_participant_path(@program_id) %>

生成される URL は次のとおりです。

http://0.0.0.0:3000/programs/1/participants/new

元のポスターによって編集:

以下からの質問とコメントに応じて:

  • リンクはインデックス ページに表示され、指定された URL に移動するはずなので、リンクは正しいように思えます。
  • 下のroutes.rbを見ると、実際にはpresent_surveyというアクションがありますが、なぜそれが機能しているのか理解できません

これがルートファイル全体です。

  root to: 'programs#index'
  resources :programs do
    resources :participants do
      resources :rounds do
        get 'survey' => 'rounds#present_survey'
        put 'survey' => 'rounds#store_survey'
      end
    end
    resources :questions
    resources :rounds
    member do
      get 'report' => 'reports#report'
    end
  end

最後に、rake ルートからの完全な出力を次に示します。

root        /                                                                                    programs#index
program_participant_round_survey GET    /programs/:program_id/participants/:participant_id/rounds/:round_id/survey(.:format) rounds#present_survey
                                 PUT    /programs/:program_id/participants/:participant_id/rounds/:round_id/survey(.:format) rounds#store_survey
      program_participant_rounds GET    /programs/:program_id/participants/:participant_id/rounds(.:format)                  rounds#index
                                 POST   /programs/:program_id/participants/:participant_id/rounds(.:format)                  rounds#create
   new_program_participant_round GET    /programs/:program_id/participants/:participant_id/rounds/new(.:format)              rounds#new
  edit_program_participant_round GET    /programs/:program_id/participants/:participant_id/rounds/:id/edit(.:format)         rounds#edit
       program_participant_round GET    /programs/:program_id/participants/:participant_id/rounds/:id(.:format)              rounds#show
                                 PUT    /programs/:program_id/participants/:participant_id/rounds/:id(.:format)              rounds#update
                                 DELETE /programs/:program_id/participants/:participant_id/rounds/:id(.:format)              rounds#destroy
            program_participants GET    /programs/:program_id/participants(.:format)                                         participants#index
                                 POST   /programs/:program_id/participants(.:format)                                         participants#create
         new_program_participant GET    /programs/:program_id/participants/new(.:format)                                     participants#new
        edit_program_participant GET    /programs/:program_id/participants/:id/edit(.:format)                                participants#edit
             program_participant GET    /programs/:program_id/participants/:id(.:format)                                     participants#show
                                 PUT    /programs/:program_id/participants/:id(.:format)                                     participants#update
                                 DELETE /programs/:program_id/participants/:id(.:format)                                     participants#destroy
               program_questions GET    /programs/:program_id/questions(.:format)                                            questions#index
                                 POST   /programs/:program_id/questions(.:format)                                            questions#create
            new_program_question GET    /programs/:program_id/questions/new(.:format)                                        questions#new
           edit_program_question GET    /programs/:program_id/questions/:id/edit(.:format)                                   questions#edit
                program_question GET    /programs/:program_id/questions/:id(.:format)                                        questions#show
                                 PUT    /programs/:program_id/questions/:id(.:format)                                        questions#update
                                 DELETE /programs/:program_id/questions/:id(.:format)                                        questions#destroy
                  program_rounds GET    /programs/:program_id/rounds(.:format)                                               rounds#index
                                 POST   /programs/:program_id/rounds(.:format)                                               rounds#create
               new_program_round GET    /programs/:program_id/rounds/new(.:format)                                           rounds#new
              edit_program_round GET    /programs/:program_id/rounds/:id/edit(.:format)                                      rounds#edit
                   program_round GET    /programs/:program_id/rounds/:id(.:format)                                           rounds#show
                                 PUT    /programs/:program_id/rounds/:id(.:format)                                           rounds#update
                                 DELETE /programs/:program_id/rounds/:id(.:format)                                           rounds#destroy
                  report_program GET    /programs/:id/report(.:format)                                                       reports#report
                        programs GET    /programs(.:format)                                                                  programs#index
                                 POST   /programs(.:format)                                                                  programs#create
                     new_program GET    /programs/new(.:format)                                                              programs#new
                    edit_program GET    /programs/:id/edit(.:format)                                                         programs#edit
                         program GET    /programs/:id(.:format)                                                              programs#show
                                 PUT    /programs/:id(.:format)                                                              programs#update
                                 DELETE /programs/:id(.:format)                                                              programs#destroy

OPによるその他の編集

  def new
    @program = Program.find(params[:program_id])
    @participant = @program.participants.new
    respond_with @participant do |format|
      format.html {
        render layout: 'layouts/progtabs'
      }
    end
  end
4

2 に答える 2

1

ルートは、ParticipantsController のメソッドにマップされるアクション「present_survey」を探しています。{:action=>"present_survey" ......} に一致するルートはありません

参加者コントローラーにpresent_surveyという名前のメソッドはありません

コマンド「rake routes」を実行して、すべてのルートのリストを順番に取得します。

あなたの URL は、ParticipantsController の新しいメソッドを探しています。ありますか?

new_program_participant GET /programs/:program_id/participants/new(.:format) 参加者#new

于 2012-11-20T00:01:06.633 に答える
0

@joofsh のおかげで、問題が解決しました。実際、ParticipantController の new メソッドによって起動された「新しい参加者」フォームには、次のものが含まれていました。

link_to("Current survey form", program_participant_round_survey_path(prog, part.guid, round))

新しい参加者の場合、part.guid は nil でした。

教訓: 文字通りルートをディスパッチしているときだけでなく、Rails にパスを生成するように要求しているときにも、ルーティング エラーが発生する可能性があります。

于 2012-11-20T19:05:36.537 に答える