#34
May 21, 2007

Named Routes

When you add a custom route, make it a named route so you have url helper methods to easily link to that new route. See episode for details.
Download (30.9 MB, 6:39)
alternative download for iPod & Apple TV (9.8 MB, 6:39)
# routes.rb
map.resources :projects
map.task_archive 'tasks/:year/:month', :controller => 'tasks', :action => 'archive'
map.home '', :controller => 'projects', :action => 'index'
<!-- projects/index.rhtml -->
<ul>
<% for project in @projects %>
  <li>
    <%= link_to project.name, project_path(project) %>
    | <%= link_to "Edit", edit_project_path(project) %>
    | <%= link_to "Destroy", project_path(project), :method => :delete %>
  </li>
<% end %>
</ul>

<p><%= link_to "New Project", new_project_path %></p>

RSS Feed for Episode Comments -10 comments

1. Jermaine May 21, 2007 at 00:40

Great episode as always!

If I'm not mistaken, you've used this kind of a named route ('resources') behind the scenes in episode 21 as well....(REST).

Gr,

- J.


2. Vladislav May 21, 2007 at 01:17

Cool!


3. Vigosan May 21, 2007 at 02:28

Congratulations,
this screencast is fabulous.


4. Ryan Bates May 21, 2007 at 07:24

@Jermaine, yep, I've been using this technique all over the place in previous episodes. Thought it was about time I explained it. :)


5. Gary May 21, 2007 at 08:26

For some reason I get a NameError whenever I try to use path but url works great. Is there any reason to believe that path is deprecated or replaced with something else? Or is there something else we need to set somewhere for this to work right?


6. Ryan Bates May 21, 2007 at 09:03

@Gary, what version of Rails are you using? It may require 1.2 but I"m not sure.


7. Geoff Buesing May 21, 2007 at 09:39

Great stuff!

Btw, Errtheblog.com has a helpful rake task to print out all of your named routes, here:

http://require.errtheblog.com/projects/browser/dugg/lib/tasks/err.rake?format=raw

So whenever I need to know what named routes are available for a resource, I just do:

rake routes | grep post


8. Ryan Bates May 21, 2007 at 11:40

That's extremely useful, thanks Geoff!

For those not sure how to install it, add the .rake file to your lib/tasks directory.


9. Gary May 22, 2007 at 08:36

I finally understood what was the problem with path not working. I had updated the gems but hadn't updated the version number in environment.rb.

Once I updated it everything worked fine.


10. Gabriel Mansour Sep 10, 2007 at 10:34

Hi Ryan,

One thing I noticed was that when

/page/1;edit
/page/edit/1


11. Mo Rashed Oct 27, 2007 at 09:36

Sup Ryan,

When you did:

map.task_archive 'tasks/:year/:month', :controller => 'tasks', :action => 'archive'

Doesnt this conflict with the standard routing:

tasks/show/2

thnxs


12. 幻想三国 Feb 02, 2010 at 23:52

I finally understood what was the problem with path not working. I had updated the gems but


13. 仙域 Feb 02, 2010 at 23:54

If I'm not mistaken, you've used this kind of a good tony


14. Joe Jul 28, 2010 at 00:19

With the routes, my named route only works if I put it right at the top, otherwise it says "undefined method"; I need to put something above it otherwise it wrecks the program, but the named route doesn't work with anything above it (although nothing is "overwriting" it); how do I fix this?

Nice work btw,

Thanks In Advance,

Joe


15. tiffany notes Jul 30, 2010 at 02:26

Great site. This could probably have the refactoring tag added t it.


16. tiffany notes Jul 30, 2010 at 02:27

Great site. This could probably have the refactoring tag added t it.


17. timberlandbootsuk Aug 02, 2010 at 02:05

we provide our buyers with an efficient and manageable procurement process covering every phase of the international supply chain and

streamlining trade channels. Also welcome wholesaling, feedback now!


18. Matt Aug 02, 2010 at 12:30

Hello Ryan,

I know this is years too late. But I was wondering if you could clear up Gabriel Mansour and Mo Rashed's questions (comments 10 and 11 respectively) It's just that those are exactly the two things I was scratching my head over.

Thanks,
Matt


19. free directory list Aug 11, 2010 at 22:08

It is marvellous! I like it!


20. free directory list Aug 11, 2010 at 22:08

It is marvellous! I like it!


21. free directory list Aug 11, 2010 at 22:08

It is marvellous! I like it!


22. p90 workout Aug 12, 2010 at 09:16

Yes, thats exactly what I wanted to hear! Great stuff here. The information and the detail were just perfect. I think that your perspective is deep, its just well thought out and really fantastic to see someone who knows how to put these thoughts down so well. Great job on this.


23. cheap air jordans Aug 22, 2010 at 23:05

Awesome post! Thanks for sharing such nice information with us. I am really interested in this learning about this. , I feel strongly about information and love learning more on this. Thanks for the information. This is a wonderful post!!


24. louis vuitton shoes Aug 26, 2010 at 21:02

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


25. snow boots Aug 31, 2010 at 01:47

my named route only works if I put it right at the top, otherwise it says "undefined method.


26. levis belts Sep 01, 2010 at 21:07

I agree with your Blog and I will be back to check it more in the future so please keep up your work. I love your content & the way that you write. It looks like you’ve been doing this for a while now, how long have you been blogging for?

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player
Give Back to Open Source