# 2.x and RESTful Controllers

**URL:** <https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980>\
**Category:** Rails\
**Created:** [August 15, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980 "2008-08-15T18:40:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 15, 2008, 6:40pm UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/1 "2008-08-15T18:40:30Z")

</div>

Hello everybody. I’m in the process of developing a site on Rails  
2.0.2, and having migrated from the early 1.x days, I’m still getting  
my feet wet with the new REST-based controllers. For the most part,  
the REST controllers generated by scaffold have been painless to use  
so far. I ran into a problem last night when I tried to add a method  
to a controller outside of the REST domain. A dumbed down example  
would be:

class CoursesController \< ApplicationController  
def hello  
render :text =\> “Hello”  
end  
end

Trying to access [http://mysite.com/courses/hello](http://mysite.com/courses/hello) would yield.

Couldn’t find Course with ID=hello

I understand why this is happening. The controller is trying to use  
hello as a course ID because of the REST URI layout, but what do I do  
if I want to define a custom method on my controller? My solution last  
night was to modify routes.rb and add something like the following  
above the map.resources :courses declaration:

map.connect ‘courses/hello’, :controller =\> ‘courses’, :action =\>  
‘hello’

This seems to work, but I’m wondering if it’s the right way to do it.  
Maybe I’m never supposed to diverge from the REST domain, but there  
are times when it’s sort of a necessity. Can anybody tell me if I’m on  
the right track, or is there a better way?

---

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 15, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/2 "2008-08-15T20:35:58Z")

</div>

Hi,

there is a better way of defining routes to custom actions in a  
RESTful controller.

I assume you have something like

map.resources :courses

in your routes.rb.

Methods can be of two types. The first is a method that is expected to  
handle a collection of the resource (like index), the other is  
expected to handle only one instance of the resource (like show and  
edit).

For a collection method you write this instead of the plain entry  
above:

map.resources :courses, :collection =\> {  
:channel\_timeline =\> :get  
}

This will create the correct route and also supply you with other  
methods like hello\_courses\_path. Set the value to :get, :post, :put  
or :delete as appropriate.

For the second type you write:

map.resources :courses, :member =\> {  
:hello =\> :get  
}

Note that this will yield different routes and methods. Now it will  
expect an id and the path method will be in singular,  
hello\_course\_path.

Hope this helps. Kind regards

Erik L.

---

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 15, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/3 "2008-08-15T20:45:55Z")

</div>

Checkout members and/or collections

I’m pretty sure the code is wrong, but it’s something like this:

map.resources :courses, :members =\> [:cancel =\> :post]  
whicih allows for /courses/1/cancel

map.resources :courses, :collections =\> [:cancel\_all]  
which allows for /courses/cancel\_all

And btw, you might as well run Rails 2.1 if you’re starting from scratch  
🙂

On Sat, Aug 16, 2008 at 12:32 AM, tmountain [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def hello  
> if I want to define a custom method on my controller? My solution last
> 
> >

–  
Ramon T.

---

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 16, 2008, 2:47am UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/4 "2008-08-16T02:47:01Z")

</div>

more information about restful routing:

> **[frozenplague.net](http://frozenplague.net/2008/01/06/restful-routing-an-overview/)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 19, 2008, 3:41am UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/5 "2008-08-19T03:41:54Z")

</div>

Hello everybody. I just wanted to say thanks for the help and for all  
the good solutions. I’m loving working with Rails so far. My only  
complaint is that it’s moderately difficult to figure out how to do  
certain things. but the community is here to help, so that’s really  
awesome.

Thanks,  
Travis

---

<div class="post-metadata">

**Author:** ![tmountain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@tmountain](https://www.ruby-forum.com/u/tmountain)\
**Post date:** [August 16, 2008, 1:56am UTC](https://www.ruby-forum.com/t/2-x-and-restful-controllers/145980/6 "2008-08-16T01:56:44Z")

</div>

Hi again

I think the syntaxes are equivalent. [:cancel =\> :post] is really  
[{:cancel =\> :post}] which has the same effect as the code in my  
answer above. The code is from an app that is in production so  
definitively works. Chose the syntax that is most comfortable to you.

Kindest regards

Erik

On 15 Aug, 20:45, “Ramon Miguel M. Tayag” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
