SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nested routes + scaffolding

    Hello all,

    I'm trying to understand nested routes at the moment.
    I generated these three models with the scaffold generator:

    Code Ruby:
    class Category < ActiveRecord::Base
      has_many :topics
    end
     
    class Topic < ActiveRecord::Base
      belongs_to :category
      has_many :posts
    end
     
    class Post < ActiveRecord::Base
      belongs_to :topic
    end
    Because all models belong together I defined the following nested routes:
    Code Ruby:
    map.resources :categories do |categories|
      categories.resources :topics do |topics|
        topics.resources :posts
      end
    end
    With these routes I have to change most of the generated views and controllers. For example instead of topic_path(params[:id]) it has to be category_topic_path(params[:category_id], params[:id]) and so on.

    Is there any way to make this easier and less error prone?

    I know it's not a good idea to have to deeply nested routes. But otherwise I don't know how to get all needed information. To create a topic for example I need the category_id ...

    Thanks for any advices.

    oerdec

  2. #2
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I made a mistake: I meant nested resources and not routes. And I found an article which might help me: Nested Resources in Rails.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •