Domanda

I have two models: team and project

routes.rb

resources :teams do
  resource :projects
end

And two questions!

1- According to http://guides.rubyonrails.org/routing.html, I expect to get teams/:team_id/projects/:id path. However, this is not the case.

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

so I had to name route to get it working

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

so how can take advantage of rails helper methods instead of naming them?


2- In project show action view, the debugger throws these parameters for me:

action: show
controller: projects
team_id: '1'

which is fine. but when I click on "new_team_projects_path" url, it redirects me to the same view and the debugger throws these parameters:

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

It doesn't redirect me to the new action, but it took "new" as an ID! why?

È stato utile?

Soluzione

  1. You need to use

    resources :teams do
      resources :projects
    end
    

    Note the plural! resource produces a singular route without id.

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top