Resource url helpers

Let say you have a model called Article and you defined a route using:

resources :articles

Here is a list of url helpers you can use:

# helper, HTTP verb, controller action
 
resource(:articles)         # GET => index
resource(@article)          # GET => show
resource(:articles, :new)   # GET => new
resource(@article, :edit)   # GET => edit
resource(@article, :delete) # GET => delete
resource(:articles)         # POST => create
resource(@article)          # PUT => update
resource(@article)          # DELETE => destroy

NOTE: Please notice the difference between @article (single form) and :articles (plural form) on the above examples.

  • @article (single form) : always as instance variable (prefixed with '@')
  • :articles (plural form) : always as symbol (prefixed with ':')

If you have a nested route such as:

resources :articles do
  resources :comments
end

Then the following url helpers are available to you:

resource(@article, :comments)         # GET => index (shows comments for @article)
resource(@article, @comment)          # GET => show
resource(@article, :comments, :new)   # GET => new
resource(@article, @comment, :edit)   # GET => edit
resource(@article, @comment, :delete) # GET => delete
resource(@article, :comments)         # POST => create (creates new comment for @article object)
resource(@article, @comment)          # PUT => update (updates comment)
resource(@article, @comment)          # DELETE => destroy (deletes comment)

These routes can be extended in a similar manner to use deeper nests if you need to, such as resource(@forum, @post, :comments).

 
development/resource_urls.txt · Last modified: 2008/12/24 05:08 by 74.66.246.62