SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    REST, routing, and overriding

    So I'm moving towards REST for one of my older apps, but there's some legacy stuff that is making things a bit more difficult. For example, this URL:

    /posts/topic/category

    (Where "posts" is the main thing we're dealing with, "topic" is broad, and "category" belongs_to "topic".) I still want to keep that URL structure even though I have three separate models here: Post, Topic, and Category. So instead of cramming everything into the Post model, I've gone with traditional RESTful means, except in separate models. For example:

    /posts/topic
    # => sends a request to my Topic controller to list all posts with the given topic.

    The problem is that /posts is restful:

    map.resources :posts

    And I can't simply re-route it like so:

    map.connect '/posts/:topic', :controller => 'topics', :action => 'index'

    ...because, well, Rails just doesn't like it. Even though I have that new route added, it still looks for the RESTful route of posts and gives me an exception.

    Any thoughts? I'm on edge Rails.

  2. #2
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use standard routes and RESTful routes at the same time, however order becomes important. Makes sure the map.connect comes BEFORE the map.resource.

    The other thing is to make sure the reg ex is right to - it looks like you want something more like /posts/:topic/:category - it might even help to tell the router that :topic and :category are string.

    Don't forget "rake routes" is your friend.

  3. #3
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I've hit on all of those aspects; my guess is that map.connect in incompatible if you're trying to override map.resources, regardless of order. It might be one of those setups where conventions don't seem to work. I might have to end up manually describing all of the RESTful routes of the posts controller with the exception of the few that I need to be slightly different.


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
  •