Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

:as option not working as expected in new Rails 3 router DSL

#4485

In my Rails 2.3.5 app I have an ArticlesController and I use the :as option in my config/routes.rb to give me paths like "/wiki/foo" instead of "/articles/foo".

I'm trying to rewrite the same routes using the new DSL and can't get the same result. These resources suggest that the same ":as => 'foo'" idiom should continue to work with resources in the new DSL:

But doesn't seem to work...

resources :articles, :as => 'wiki'

Yields:

          GET    /articles(.:format)                                            {:controller=>"articles", :action=>"index"}
    wikis POST   /articles(.:format)                                            {:controller=>"articles", :action=>"create"}
 new_wiki GET    /articles/new(.:format)                                        {:controller=>"articles", :action=>"new"}
          GET    /articles/:id(.:format)                                        {:controller=>"articles", :action=>"show"}
          PUT    /articles/:id(.:format)                                        {:controller=>"articles", :action=>"update"}
     wiki DELETE /articles/:id(.:format)                                        {:controller=>"articles", :action=>"destroy"}
edit_wiki GET    /articles/:id/edit(.:format)                                   {:controller=>"articles", :action=>"edit"}

Compare that with the output for 2.3.5, where:

map.resources :articles,:as => :wiki

Yields:

    articles GET    /wiki(.:format)                                                {:controller=>"articles", :action=>"index"}
             POST   /wiki(.:format)                                                {:controller=>"articles", :action=>"create"}
 new_article GET    /wiki/new(.:format)                                            {:controller=>"articles", :action=>"new"}
edit_article GET    /wiki/:id/edit(.:format)                                       {:controller=>"articles", :action=>"edit"}
     article GET    /wiki/:id(.:format)                                            {:controller=>"articles", :action=>"show"}
             PUT    /wiki/:id(.:format)                                            {:controller=>"articles", :action=>"update"}
             DELETE /wiki/:id(.:format)                                            {:controller=>"articles", :action=>"destroy"}

In other words, under 2.3.5 the :as option on a resource causes the path in the URL to change but not the names of the generated URL helper methods.

Under 3.0 the :as option on a resource does the opposite, changing the names of the generated URL helper methods but not the path in the URL itself.

Is this intended behavior (in which case we're probably talking about a documentation bug) or an implementation bug? Or am I misreading the documentation and should be doing this in another way?

Reported by Wincent Colaiuta · April 27th, 2010 @ 08:09 AM

State: invalid
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. Wincent Colaiuta
    Wincent Colaiuta

    Can someone involved on the API design comment on this one? If I can just get some clarification on whether this is a doc bug, an implementation bug, or user error, then I can work on a fix.

    April 28th, 2010 @ 06:32 PM

  2. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to invalid

    Wincent as changes the named route only i'm going to change the guides, thanks ;).

    April 29th, 2010 @ 03:59 PM

  3. José Valim
    José Valim
    • Milestone cleared.
    • Assigned user set to José Valim

    It's intended behavior. Although not consistent with the previous API, the new router API is consistent internally in a sense that:

    • :path always changes the path
    • :as always changed the named url helper

    April 29th, 2010 @ 04:00 PM

  4. Wincent Colaiuta
    Wincent Colaiuta

    Cool. :path does exactly what I want/need.

    BTW I haven't seen the ":path" parameter mentioned anywhere. The official guide only mentions ":path_names", as does Yehuda's article.

    April 29th, 2010 @ 04:40 PM

  5. José Valim
    José Valim

    Yeah, the DSL was inconsistent and we fixed and added :path just in beta 3.

    April 29th, 2010 @ 04:41 PM

  6. Wincent Colaiuta
    Wincent Colaiuta

    Well, it certainly looks more consistent now. Good job on that. (In 2.3.5 I had a fairly ugly routes file, about 100 lines long. With the new DSL it's still almost the same length, but it's much clearer now and the amount of repetition and crud is greatly reduced.)

    April 29th, 2010 @ 04:46 PM

  7. José Valim
    José Valim

    Awesome, thanks for the feedback! :)

    April 29th, 2010 @ 04:47 PM

  8. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM