当路由在Rake Routes的输出中列出时获取ActionController :: RoutingError

时间:2013-03-20 14:13:12

标签: ruby-on-rails ruby ruby-on-rails-3

我有模型DrillExercise。练习属于Drills,我有routes.rb:

resources :drills do
    resources :exercises do
end

因此,从rake路线看到这个输出并不奇怪:

drill_exercises GET    /drills/:drill_id/exercises(.:format)                                      exercises#index
                              POST   /drills/:drill_id/exercises(.:format)                                      exercises#create
           new_drill_exercise GET    /drills/:drill_id/exercises/new(.:format)                                  exercises#new
          edit_drill_exercise GET    /drills/:drill_id/exercises/:id/edit(.:format)                             exercises#edit
               drill_exercise GET    /drills/:drill_id/exercises/:id(.:format)                                  exercises#show
                              PUT    /drills/:drill_id/exercises/:id(.:format)                                  exercises#update
                              DELETE /drills/:drill_id/exercises/:id(.:format)                                  exercises#destroy

令人惊讶的是这行代码:

<%= link_to t('.new', :default => t("helpers.links.prompt")),
      new_drill_exercise_path,
      :class => 'btn btn-primary', :remote => true %>

导致此错误:

ActionController::RoutingError at /drills/6/edit

没有路线匹配{:action =&gt;&#34; new&#34;,:controller =&gt;&#34;练习&#34;}

尽管事实上当我在IRB中调用controller.methods时,我得到的结果之一是:new_drill_exercise_path

那么......那是什么?

更多信息:

exercises_controller.rb

class ExercisesController < InheritedResources::Base
  def new
    @drill = Drill.find(params[:id])
    @exercise = Exercise.new
    respond_to do |format|
      format.html { redirect_to(:action => 'edit')  }
      format.js 
    end 
  end
end

1 个答案:

答案 0 :(得分:1)

您错过了路径所需的drill_id。尝试:

new_drill_exercise_path(@drill)

或:

new_drill_exercise_path(params[:id]) # assumes this is inside `DrillsController#show` or similar