# Controller Problem

**URL:** <https://www.ruby-forum.com/t/controller-problem/152816>\
**Category:** Rails\
**Created:** [November 7, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/controller-problem/152816 "2008-11-07T18:11:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 6:11pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/1 "2008-11-07T18:11:37Z")

</div>

I have this action in questions\_controller.rb :

def arg  
@questions = Question.find(:all)

respond\_to do |format|  
format.html  
format.xml { render :xml =\> @questions }  
end  
end

Why this action doesn’t work ?  
It is the same as index action, but this one doesn’t work.  
The error is: “Couldn’t find Question with ID=arg”

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 7:16pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/2 "2008-11-07T19:16:00Z")

</div>

> The error is: “Couldn’t find Question with ID=arg”

What’s the url that is supposed to be invoking the arg action?

What do your routes look like?

rake routes \>routes.lst

then browse that file to see the routes that are available

it seems that ‘arg’ is being interpreted as an id parameter for an  
existing route…

if you want arg as an action for the whole collection, then I’d expect  
to see something like

map.resources :questions, :collection =\> {:arg =\> :get}

in routes.rb to add that as a recognized route

If it’s for a specific question, then

map.resources :questions, :member =\> {:arg =\> :get}

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 7:43pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/3 "2008-11-07T19:43:29Z")

</div>

> What’s the url that is supposed to be invoking the arg action?

[http://localhost:3000/questions/arg](http://localhost:3000/questions/arg)

> if you want arg as an action for the whole collection, then I’d expect  
> to see something like
> 
> map.resources :questions, :collection =\> {:arg =\> :get}

That way it works. But can you explain me the reason ?  
What’s the meaning of the parameter :collection =\> {:arg =\> :get} ?

Every time i create a new action, I have to update the routes.rb file ??

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 7:59pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/4 "2008-11-07T19:59:15Z")

</div>

Davide S. wrote:

> > What’s the url that is supposed to be invoking the arg action?
> 
> [http://localhost:3000/questions/arg](http://localhost:3000/questions/arg)
> 
> > if you want arg as an action for the whole collection, then I’d expect  
> > to see something like
> > 
> > map.resources :questions, :collection =\> {:arg =\> :get}
> 
> That way it works. But can you explain me the reason ?  
> What’s the meaning of the parameter :collection =\> {:arg =\> :get} ?
> 
> Every time i create a new action, I have to update the routes.rb file ??

That is true, yes. The RESTful design methodology requires you to define  
custom actions in your routes file. That is because earlier when we used  
map.connect ‘:controller/:action/:id’ we always used the GET method -  
for the destroy, create and update actions.

Then someone found out that you could make the urls prettier (and  
respect the RESTful design philosophy) by using other standard HTTP  
methods like DELETE, POST and PUT. Therefore you have to actually tell  
Rails what method you’ld like to use, it will not use GET by default.  
That is actually good. Imagine having half of your custom actions being  
using the GET method, the other half using the POST, PUT or DELETE  
method. Then you’ld need to define what method to use for half of them  
in your routes file (instead of all of them). What a mess… (:

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/5 "2008-11-07T20:11:20Z")

</div>

> That is true, yes. The RESTful design methodology requires you to define  
> custom actions in your routes file. That is because earlier when we used  
> map.connect ‘:controller/:action/:id’ we always used the GET method -  
> for the destroy, create and update actions.
> 
> Then someone found out that you could make the urls prettier (and  
> respect the RESTful design philosophy) by using other standard HTTP  
> methods like DELETE, POST and PUT. Therefore you have to actually tell  
> Rails what method you’ld like to use, it will not use GET by default.  
> That is actually good. Imagine having half of your custom actions being  
> using the GET method, the other half using the POST, PUT or DELETE  
> method. Then you’ld need to define what method to use for half of them  
> in your routes file (instead of all of them). What a mess… (:

So, if i don’t specify a method (as we did), the default one would be  
POST ??  
Or DELETE ??  
Or PUT ??

Which one ??

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/6 "2008-11-07T20:52:49Z")

</div>

Davide S. wrote:

> > That is true, yes.

> So, if i don’t specify a method (as we did), the default one would be  
> POST ??  
> Or DELETE ??  
> Or PUT ??
> 
> Which one ??

Sorry for being a little unclear but this was my point: Rails will NOT  
try to guess which method you want and there is no default. In other  
words, since we don’t only use GET now, we have to tell Rails which one  
to use (no matter if it’s GET, POST, PUT or DELETE).

map.resources :questions, :collection =\> {:arg =\> :get}

In the example above we pass an option called :collection that specifies  
which custom actions that is going to affect a _collection_ of (in your  
case) questions. Then we define one custom action (“arg”) that is going  
to use the GET method. See the point? :arg =\> :get

The collection option accepts a hash with action name as key and method  
name (GET, POST, PUT, DELETE) as value, so there is no default. Also  
remember that if you want to make an action that is only going to affect  
_one_ question you’ll want to use the :member option instead, like this:

map.resources :questions, :collection =\> {:arg =\> :get}, :member =\>  
{:affecting\_one\_question =\> :get}

Hope that helped. (:

---

<div class="post-metadata">

**Author:** ![kerouak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/6de8d8/32.png) [@kerouak](https://www.ruby-forum.com/u/kerouak)\
**Post date:** [November 7, 2008, 9:49pm UTC](https://www.ruby-forum.com/t/controller-problem/152816/7 "2008-11-07T21:49:34Z")

</div>

> That way it works. But can you explain me the reason ?  
> What’s the meaning of the parameter :collection =\> {:arg =\> :get} ?
> 
> Every time i create a new action, I have to update the routes.rb file ??

[http://api.rubyonrails.org/classes/ActionController/Resources.html](http://api.rubyonrails.org/classes/ActionController/Resources.html)
