【问题标题】:ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"educations"}ActionController::UrlGenerationError: 没有路由匹配 {:action=>"index", :controller=>"educations"}
【发布时间】:2017-06-19 18:27:20
【问题描述】:

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

即使在我对 get :index 的最基本测试中,我也遇到了错误:

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

我看过有关显示/编辑/更新需要 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

您认为这与嵌套在应用程序路由中的教育路由有关吗?

我正在使用设计。那里会不会有冲突?

感谢您的任何建议!

【问题讨论】:

  • 控制器是否已经制作完毕,并且上面列出了索引操作?
  • 如果您运行rake routes,是否会出现GET /educations

标签: ruby-on-rails rspec devise


【解决方案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_educations_path 中包含一个 @application 实例,或者 (2) 取消嵌套路由。

【讨论】:

  • 谢谢@jvillian!这无疑帮助我进行了测试。对于任何关注的人,我遇到的下一个障碍是使用 Devise 登录:我将 => sign_in create(:user)
  • 太棒了。如果您认为合适,请随意投票或接受。
  • 我确实为你投票,但我是超级新人,所以它不会显示。 (显然它被记录了?)@jvillian
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多