ActionController :: UrlGenerationError:没有路由匹配{:action =>" index",:controller =>" educations"}

时间:2017-06-19 18:27:21

标签: ruby-on-rails rspec devise

我正在使用RSpec在控制器spec/controllers/educations_controller_spec.rb上运行测试。

即使在我最基本的get :index测试中,我收到错误:

ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"educations"}

我看过关于show / edit / update的帖子,其中需要id,但我不认为索引操作需要id。

在spec / controllers / educations_controller_spec.rb中:

require 'rails_helper'

RSpec.describe EducationsController, type: :controller do
  describe 'GET #index' do
    it "renders the :index template" do
      get :index
      expect(response).to render_template :index
    end
  end
end

我的路线文件包含此索引操作:

resources :applications, only: [:index, :new, :show, :create, :edit, :update] do
    resources :educations, only: [:index, :new, :create, :edit, :update]
    resources :montessori_trainings, only: [:index, :new, :create, :edit, :update]
    resources :work_experiences, only: [:index, :new, :create, :edit, :update]
    resources :references, only: [:index, :new, :create, :edit, :update]
    resources :documents, only: [:index, :new, :create, :edit, :update]
end

您是否认为它与嵌套在应用程序路径中的教育路径有关?

我正在使用Devise。那里会发生冲突吗?

感谢您提供任何建议!

1 个答案:

答案 0 :(得分:1)

完全与嵌套在应用程序路径中的教育路径有关。如果您rake routes,您会看到(针对教育):

      application_educations GET    /applications/:application_id/educations(.:format)                    educations#index
                             POST   /applications/:application_id/educations(.:format)                    educations#create
   new_application_education GET    /applications/:application_id/educations/new(.:format)                educations#new
  edit_application_education GET    /applications/:application_id/educations/:id/edit(.:format)           educations#edit
       application_education PATCH  /applications/:application_id/educations/:id(.:format)                educations#update
                             PUT    /applications/:application_id/educations/:id(.:format)                educations#update

因此,您的教育路线期待application_id

您需要:(1)在@application中包含application_educations_path个实例,或者(2)在路线中取消嵌套。