# Named Route question

**URL:** <https://www.ruby-forum.com/t/named-route-question/154346>\
**Category:** Rails\
**Created:** [November 28, 2008, 1:40am UTC](https://www.ruby-forum.com/t/named-route-question/154346 "2008-11-28T01:40:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 1:40am UTC](https://www.ruby-forum.com/t/named-route-question/154346/1 "2008-11-28T01:40:56Z")

</div>

I have in my routes.rb

map.resources :accounts

This is fine for the standard crud routes.

But then I add a method in the accounts\_controller

def logout  
blah blah  
end

I want to link to it so I do:

\<%=link\_to ‘Logout’ ,:controller=\>‘accounts’, :action=\>‘logout’%\>  
which creates:  
[Logout](https://www.ruby-forum.com/accounts/logout)

But it just calls the ‘show’ method of the accounts\_controller and  
trys to find an account with an id of ‘logout’

What am I missing here?

How can I call the logout method.  
It doesn’t seem to be a problem if I dont specify a controller, but in  
this case the link is in the layout and needs to specify the  
controller.

Cheers  
George

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 2:08am UTC](https://www.ruby-forum.com/t/named-route-question/154346/2 "2008-11-28T02:08:25Z")

</div>

On Nov 27, 2008, at 4:40 PM, giorgio wrote:

> blah blah
> 
> What am I missing here?

map.resources :accounts, :collection =\> {:get =\> :logout}

That should give you a accounts\_logout\_path named route you can use.

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 2:30am UTC](https://www.ruby-forum.com/t/named-route-question/154346/3 "2008-11-28T02:30:38Z")

</div>

That doesn’t work.

I can do:  
map.logout ‘logout’, :controller =\> ‘accounts’, :action =\> ‘logout’  
Not very elegant though  
But what is the point of having the default routes if you cant get to  
them?

Is the idea to make a named route for everything?

G

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 2:41am UTC](https://www.ruby-forum.com/t/named-route-question/154346/4 "2008-11-28T02:41:10Z")

</div>

On Nov 27, 2008, at 5:29 PM, giorgio wrote:

> That doesn’t work.

I had it backwards.

Try this.

map.resources :accounts, :collection =\> {:logout =\> get}

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 2:52am UTC](https://www.ruby-forum.com/t/named-route-question/154346/5 "2008-11-28T02:52:51Z")

</div>

map.resources :accounts, :collection =\> {:logout =\> :get}

works…(:get not get)

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [November 28, 2008, 6:21am UTC](https://www.ruby-forum.com/t/named-route-question/154346/6 "2008-11-28T06:21:35Z")

</div>

You can make named routes for everything using the technique Phillip  
posted, you could rearrange your actions to be more restful (maybe a  
sessions controller, and map login to sessions#create, or move your  
default routes to a different position in the routes file (order is  
important here).
