RSpec 嵌套资源的路由错误
RSpec Routing Error w/ Nested Resources
我认为应该毫无问题地通过规范,但我认为由于它是嵌套资源,它可能会拒绝我的请求。我正在使用 Rails 4.2 和 Ruby 2.3.3。这是怎么回事?我知道这是一条有效的路线,因为当我点击 rake routes
时它会出现
routes.rb
scope '/organizations/:organization_id' do
get 'dashboard', to: 'projects#dashboard'
resources :projects, except: [:delete] do
get 'configure', to: 'projects#configure'
post 'configure', to: 'projects#configure'
get 'team', to: 'projects#team'
get 'subprojects', to: 'projects#subprojects'
collection do
get 'search', to: 'projects#search'
get 'find', to: 'projects#find'
post 'create_by_sf_id', to: 'projects#create_by_sf_id'
end
resources :courses do
get 'module_progress', to: 'courses#module_progress'
get 'add_content', to: 'courses#add_content'
get 'summary', to: 'courses#summary'
post 'summary', to: 'courses#summary'
end
resources :tasks
end
end
projects_controller_spec.rb
describe 'GET project_team' do
it 'should render the project team page' do
get :team, organization_id: organization.id, id: project.id
expect(response.code).to eq '200'
end
end
projects_controller.rb
def team
@team = @project.project_team
end
...Aaa 以及由此产生的错误:
1) ProjectsController when authenticating as a customer GET project_team should render the project team page
Failure/Error: get :team, organization_id: organization.id, id: project.id
ActionController::UrlGenerationError:
No route matches {:action=>"team", :controller=>"projects", :id=>"460", :organization_id=>"417"}
# ./spec/controllers/projects_controller_spec.rb:90:in `block (4 levels) in <top (required)>'
你有 team
嵌套在 projects
下,这给你:
project_team GET /organizations/:organization_id/projects/:project_id/team(.:format) projects#team
所以这个:
get :team, organization_id: organization.id, id: project.id
大概应该是这样的:
get :team, organization_id: organization.id, project_id: project.id
看来您可能会遇到麻烦:
def team
@team = @project.project_team
end
因为你不查找 @project
(除非你在 before_action
钩子中查找)。
最后,我想创建一个 ProjectTeamController
。这样,您就可以使用 show
方法而不是非标准的 team
方法。但是,这是个人喜好问题。
我认为应该毫无问题地通过规范,但我认为由于它是嵌套资源,它可能会拒绝我的请求。我正在使用 Rails 4.2 和 Ruby 2.3.3。这是怎么回事?我知道这是一条有效的路线,因为当我点击 rake routes
routes.rb
scope '/organizations/:organization_id' do
get 'dashboard', to: 'projects#dashboard'
resources :projects, except: [:delete] do
get 'configure', to: 'projects#configure'
post 'configure', to: 'projects#configure'
get 'team', to: 'projects#team'
get 'subprojects', to: 'projects#subprojects'
collection do
get 'search', to: 'projects#search'
get 'find', to: 'projects#find'
post 'create_by_sf_id', to: 'projects#create_by_sf_id'
end
resources :courses do
get 'module_progress', to: 'courses#module_progress'
get 'add_content', to: 'courses#add_content'
get 'summary', to: 'courses#summary'
post 'summary', to: 'courses#summary'
end
resources :tasks
end
end
projects_controller_spec.rb
describe 'GET project_team' do
it 'should render the project team page' do
get :team, organization_id: organization.id, id: project.id
expect(response.code).to eq '200'
end
end
projects_controller.rb
def team
@team = @project.project_team
end
...Aaa 以及由此产生的错误:
1) ProjectsController when authenticating as a customer GET project_team should render the project team page
Failure/Error: get :team, organization_id: organization.id, id: project.id
ActionController::UrlGenerationError:
No route matches {:action=>"team", :controller=>"projects", :id=>"460", :organization_id=>"417"}
# ./spec/controllers/projects_controller_spec.rb:90:in `block (4 levels) in <top (required)>'
你有 team
嵌套在 projects
下,这给你:
project_team GET /organizations/:organization_id/projects/:project_id/team(.:format) projects#team
所以这个:
get :team, organization_id: organization.id, id: project.id
大概应该是这样的:
get :team, organization_id: organization.id, project_id: project.id
看来您可能会遇到麻烦:
def team
@team = @project.project_team
end
因为你不查找 @project
(除非你在 before_action
钩子中查找)。
最后,我想创建一个 ProjectTeamController
。这样,您就可以使用 show
方法而不是非标准的 team
方法。但是,这是个人喜好问题。