Ruby on Rails 3嵌套路由:错误的动作路由

时间:2012-07-21 02:12:30

标签: ruby-on-rails-3 nested-routes

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

的routes.rb

resources :teams do
  resource :projects
end

还有两个问题!

1 - 根据http://guides.rubyonrails.org/routing.html,我希望获得teams /:team_id / projects /:id路径。但事实并非如此。

rake routes

         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 helper方法而不是命名呢?


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

action: show
controller: projects
team_id: '1'

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

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

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

1 个答案:

答案 0 :(得分:4)

  1. 您需要使用

    resources :teams do
      resources :projects
    end
    

    注意复数! resource生成一条没有id的单一路线。

  2. 与第一次修复不再相关。