Forum: Ruby on Rails nested resources

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F87d150876b608711d5fc8154a8038c9?d=identicon&s=25 Herman Müller (gotzbold)
on 2008-11-01 16:36
Hello,

I have a problem with nested resources.

Ich made following entries in the routes.rb:

  map.resources :projects do |project|
    project.resources :iterations do |iteration|
      iteration.resources :tasks
    end
  end

when I use link_to:

<%= link_to 'Show', project_iteration_task_path(task.iteration.project,
task) %>

following is displayed:

http://localhost:3000/projects/2/iterations/117/tasks/32

The problem is, that the IDs of iterations and task are interchanged.
The real iteration_id = 32 and the real task_id = 117.

do sombebody knows a solution or what's worn with the code?

Thanks for every answer!

Hermann
30a673ff3a350dd5c7c93ad410f4ed6e?d=identicon&s=25 Rudi Indemans (Guest)
on 2008-11-01 17:36
(Received via mailing list)
Herman Müller schreef:
>   end
> The problem is, that the IDs of iterations and task are interchanged.
> The real iteration_id = 32 and the real task_id = 117.
>
> do sombebody knows a solution or what's worn with the code?
>
> Thanks for every answer!
>
> Hermann
>
Hermann

You can try the alternative syntax:

map.resources  :tasks
map.resources  :iterations, :has_many => [:tasks]
map.resources  :projects, :has_many => [:iterations]

Rudi
5eb57ea1f36182bdd64e5601a73262e6?d=identicon&s=25 Nellboy (Guest)
on 2008-11-01 19:21
(Received via mailing list)
isn't it safer to use the models to define the relationships?
F87d150876b608711d5fc8154a8038c9?d=identicon&s=25 Herman Müller (gotzbold)
on 2008-11-01 20:20
Hello Rudi,

I've got an answer form the german forum:

The solution from Thomas Baustert,

project_iteration_task_path(task.iteration.project, task.iteration,
task)

and it works fine:-)

I only wanted to generate better readable URLS like:

http://localhost:3000/projects/2/iterations/36/tasks/140

When I used

   map.resources :projects, :has_many => [ :iterations]

   map.resources :iterations, :has_many => [ :tasks]

the same URL for tasks looked like this:

http://localhost:3000/iterations/36/tasks/140

but there is no conclusion to the project_id.

Thank you for your answer

Regs,

Herman
This topic is locked and can not be replied to.