• Home
  • About

stonean

Feeds:
Posts
Comments

Rails Routes

August 14, 2008 by stonean

I don’t frequently have the need to create anything more than the basic “map.resources :model” route. As things go, this means I don’t remember how to anything other than the basic routes and forget the difference between :collection and :member and how that translates to urls and helper methods. So, I’m creating this page as a reference for myself so I don’t have to google and piece together bits of information.

Basic:

map.resources :users

# Which gives you the following helpers:
users_path
users_url
new_user_path
new_user_url
user_path(@user)
user_url(@user)
edit_user_path(@user)
edit_user_url(@user)

# Accessed via:
/users
/users/new
/users/1
/users/1/edit

Nested:

map.resources :posts do |posts|
  posts.resources :comments
end

# Which gives you the following helpers:
post_comments_path
post_comments_url
new_post_comment_path
new_post_comment_url
post_comment_path(@post,@comment)
post_comment_url(@post,@comment)
edit_post_comment_path(@post,@comment)
edit_post_comment_url(@post,@comment)

# Accessed via:
post/1/comments
post/1/comments/new
post/1/comments/1
post/1/comments/1/edit

Adding methods outside the standard REST methods.

map.resources :users, :collection => {:active => :get}

# Which gives you the following helpers:

active_users_path
active_users_url

# Accessed via:

/users/active

The member option means you’re going to access a ‘member’ of the collection. Why this didn’t stick the first time, I have no idea. I’m just slow I guess.

map.resources :users, :member => {:activate => :post}

# Which gives you the following helpers:
activate_user_path(@user)
activate_user_url(@user)

#Accessed via:

/users/1/activate

There will be more added as I run across more usage needs. Let me know if I messed anything up here.

Posted in RubyOnRails | 1 Comment

One Response

  1. on October 17, 2008 at 8:00 pm Anonymous

    thank you soooo much.



Comments are closed.

  • Open Source


    Lockdown (GitHub)
    RuHL (GitHub)

  • Recommend Me

  • twitter: stonean

    • I checked my RailsConf proposal status and it's 'under review'. 'Dumb and Dumber' then came to mind: 'so there's a chance, *yeah*'. 19 hours ago
    • Yet another way to waste time in the guise of keeping up to date: http://vurl.me/EIR 3 days ago
    • So I submitted my RailsConf proposal, called it Beyond MVC. We shall see. I do such a bad job talking about myself, the bio sucked. 4 days ago
    • @ubermuda that's good to know, thank you! 5 days ago
    • @chanmix51 cool, thanks! 5 days ago
  • Categories

    classy-inheritance Code Style DataMapper Fusion git Haml Interview Questions jQuery lockdown Merb MysqlUtils Process Quick Tips REST RSpec Ruby RubyOnRails rubytrends Stage thoughts Uncategorized


  • Archives

    • November 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007

Blog at WordPress.com.

Theme: Mistylook by Sadish.