0

我有两个模型:团队和项目

路线.rb

resources :teams do
  resource :projects
end

还有两个问题!

1-根据http://guides.rubyonrails.org/routing.html,我希望得到 team/:team_id/projects/:id 路径。然而,这种情况并非如此。

耙路线

         team_projects POST     /teams/:team_id/projects(.:format)      projects#create
     new_team_projects GET      /teams/:team_id/projects/new(.:format)  projects#new
    edit_team_projects GET      /teams/:team_id/projects/edit(.:format) projects#edit
                       GET      /teams/:team_id/projects(.:format)      projects#show
                       PUT      /teams/:team_id/projects(.:format)      projects#update
                       DELETE   /teams/:team_id/projects(.:format)      projects#destroy

所以我必须命名路线才能让它工作

  match 'teams/:team_id/projects/:id' => 'projects#show', :via => [:get], :as => :show_project

那么如何利用 rails 辅助方法而不是命名它们呢?


2- 在项目显示操作视图中,调试器为我抛出这些参数:

action: show
controller: projects
team_id: '1'

这很好。但是当我点击“new_team_projects_path”网址时,它会将我重定向到同一个视图,并且调试器会抛出这些参数:

controller: projects
action: show
team_id: '1'
id: new

它不会将我重定向到新操作,而是将“新”作为 ID!为什么?

4

1 回答 1

4
  1. 你需要使用

    resources :teams do
      resources :projects
    end
    

    注意复数!resource产生一条没有 id 的奇异路线。

  2. won't be relevant anymore with the first fix.

于 2012-07-21T02:36:00.883 回答