复杂的Rails 3嵌套路由

时间:2012-11-19 23:47:42

标签: ruby-on-rails routing

当我点击“创建参与者”按钮时,我得到了这个奇怪的奇怪路由错误。显然,我误解了溃败的工作方式。如果你能澄清那将是值得赞赏的!

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) %>

这是生成的网址:

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

2 个答案:

答案 0 :(得分:1)

路线正在寻找行动“present_survey”,它映射到ParticipantsController中的方法。 没有路线匹配{:action =&gt;“present_survey”......}

ParticipantsController中没有名为present_survey的方法

运行命令“rake routes”按顺序获取所有路线的列表。

您的网址是在ParticipantsController中寻找新方法,您有吗?

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

答案 1 :(得分:0)

感谢@joofsh,我发现了我的问题。事实上,新方法在ParticipantController上推出的“新参与者”形式包括:

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

对于新参与者,part.guid是零。

课程:当您要求Rails为您生成路径时,您可以获得路由错误,而不仅仅是在您实际调度路径时。