0

我有一个我觉得应该毫无问题地通过的规范,但我相信,因为它是一个嵌套资源,它可能会抛出我的请求。我正在使用带有 Ruby 2.3.3 的 Rails 4.2。这是怎么回事?我知道这是一条有效的路线,因为当我击球时它出现rake 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

项目控制器.rb

def team
  @team = @project.project_team
end

...Aaaand 产生的错误:

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)>'
4

1 回答 1

1

你已经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方法。但是,这是个人喜好问题。

于 2017-05-24T21:07:01.167 回答