简体   繁体   English

复杂的Rails 3嵌套路线

[英]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: 这是相关的routes.rb行:

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: 这是生成的URL:

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. 该链接显示在索引页面上,并且应该带我到指示的URL,因此该链接对我来说似乎是正确的。
  • 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 您可以在下面的route.rb中看到确实存在一个名为present_survey的动作,但我不明白为什么它在起作用

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 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. Route正在寻找动作“ present_survey”,该动作映射到ParticipantsController中的方法。 No route matches {:action=>"present_survey" ......} 没有路线符合{:action =>“ present_survey” ......}

There is no method named present_survey in ParticipantsController ParticipantsController中没有名为present_survey的方法

Run command "rake routes" to get the list of all routes in order. 运行命令“ rake route”以按顺序获取所有路由的列表。

Your URL is looking for new method in ParticipantsController, do you have one? 您的URL在ParticipantsController中寻找新方法,您有吗?

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

Thanks to @joofsh I figured out my problem. 多亏@joofsh,我才知道我的问题。 Indeed the "new participant" form launched by the new method on ParticipantController included: 实际上,由ParticipantController上的新方法启动的“新参与者”表单包括:

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

and for a new Participant the part.guid was nil. 对于新参与者,part.guid为零。

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. 课程:当您要求Rails为您生成路径时,不仅在字面上是分派路由时,也会出现路由错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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