简体   繁体   中英

Getting ActionController::RoutingError when the route is listed in output of Rake Routes

I have models Drill and Exercise . Exercises belong to Drills and I have routes.rb with:

resources :drills do
    resources :exercises do
end

Therefore it is not surprising to see this output from rake routes:

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

What is surprising is that this line of code:

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

Is resulting in this error:

ActionController::RoutingError at /drills/6/edit

No route matches {:action=>"new", :controller=>"exercises"}

Despite the fact that when I call controller.methods in IRB one of the results I get back is :new_drill_exercise_path

So... what's up with that?

More info:

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

You are missing the drill_id that is required for the path. Try:

new_drill_exercise_path(@drill)

or:

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

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