简体   繁体   中英

Complicated Rails 3 Nested route

I get this weird weird routing error when I click on the "Create Participant" button. Clearly I am misunderstanding something about how routs work. If you could clarify that would be appreciated!

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">}

Here's the relevant routes.rb line:

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

Here are the relevant controller lines:

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

Here's the relevant view line:

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

Here's the url that is generated:

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

edited by original poster:

In response to questions and comments from below:

  • The link is appearing on the index page, and is supposed to take me to the indicated URL, so the link seems correct to me.
  • You can see in the routes.rb below that indeed I have an action called present_survey but I can't understand why it is in play

Here is the whole routes file:

  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

And finally here's the full output from rake routes:

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

more edits by 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

Route is looking for action "present_survey" which maps to method in ParticipantsController. No route matches {:action=>"present_survey" ......}

There is no method named present_survey in ParticipantsController

Run command "rake routes" to get the list of all routes in order.

Your URL is looking for new method in ParticipantsController, do you have one?

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

Thanks to @joofsh I figured out my problem. Indeed the "new participant" form launched by the new method on ParticipantController included:

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

and for a new Participant the part.guid was nil.

Lesson: You can get a routing error also when you are asking Rails to generate a path for you, not only when you are literally dispatching a route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM