# Possible to change controller's default action?

**URL:** <https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046 "2005-11-12T12:50:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/1 "2005-11-12T12:50:12Z")

</div>

Is it possible to change a controller’s default action

- say from “index” to “list”?

[mydomain.com/item](http://mydomain.com/item) (I want it to “list” rather than  
“index”)

thanks  
csn

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/2 "2005-11-12T12:50:12Z")

</div>

Hi !

2005/11/11, CSN [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Is it possible to change a controller’s default action
> 
> - say from “index” to “list”?

Redirect to :list, or render :list should do it !

If you look at scaffolded code, the second solution is the one used.

Hope that helps !  
FranÃ§ois

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/3 "2005-11-12T12:50:13Z")

</div>

Right, but I have different templates (displaying  
different stuff) for index and list.

csn

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 12, 2005, 12:50pm UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/4 "2005-11-12T12:50:13Z")

</div>

On Nov 11, 2005, at 10:27 PM, Francois B. wrote:

> Hi !
> 
> 2005/11/11, CSN [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > Is it possible to change a controller’s default action
> > 
> > - say from “index” to “list”?
> 
> Redirect to :list, or render :list should do it !
> 
> If you look at scaffolded code, the second solution is the one used.

You can also set up a route:

map.connect “special/:action”, :controller =\> “special”, :action  
=\> “list”

- Jamis

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 14, 2005, 8:59am UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/5 "2005-11-14T08:59:45Z")

</div>

On 12.11.2005, at 10.15, CSN wrote:

> Right, but I have different templates (displaying  
> different stuff) for index and list.

So you have both actions, index and list, that do different things.  
However, you don’t want the action index to be the default one. Am I  
right?

I don’t really understand why, since index is _the_ name for a  
default file of a “folder”. If you use page caching, you will  
probably run into trouble for this exact reason, even if you use the  
trick I’m about to tell you. So caveat emptor.

Put this into your routes:

map.connect “special/”, :controller =\> “special”, :action =\> “list”

//jarkko

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 14, 2005, 11:06am UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/6 "2005-11-14T11:06:35Z")

</div>

Well, I just wanted my URLs to be as so:

[mysite.com](http://mysite.com) → item’s index action (which has different  
queries and layout than ‘list’)  
[mysite.com/items](http://mysite.com/items) → item’s list action  
[mysite.com/item/id](http://mysite.com/item/id) → item’s show action

and there are other URLs:

[mysite.com/member](http://mysite.com/member) → member’s index/show action (uses  
session)  
[mysite.com/members](http://mysite.com/members) → member’s list action

This is likely achievable with routes, and I briefly  
tried, but it became complicated (I didn’t know there  
would be caching issues either, which I haven’t gotten  
into yet).

csn

— Jarkko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> right?
> 
> > > Redirect to :list, or render :list should do it !
> > > 
> > > If you look at scaffolded code, the second  
> > > solution  
> > > is the one used.
> > > 
> > > Hope that helps !  
> > > François

craig

* * *

Yahoo! FareChase: Search multiple travel sites in one click.  
[http://farechase.yahoo.com](http://farechase.yahoo.com)

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 14, 2005, 11:06am UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/7 "2005-11-14T11:06:35Z")

</div>

On 14.11.2005, at 11.39, CSN wrote:

> [mysite.com/member](http://mysite.com/member) → member’s index/show action (uses  
> session)  
> [mysite.com/members](http://mysite.com/members) → member’s list action
> 
> This is likely achievable with routes, and I briefly  
> tried, but it became complicated (I didn’t know there  
> would be caching issues either, which I haven’t gotten  
> into yet).

There shouldn’t be any issues with the url’s described above. They’re  
just url’s and you can map them to controllers/actions using routes.  
What do you mean by it becoming complicated? If you want that the  
url’s in your app behave in some other way than the  
normal :controller/:action/:id, you need to tell it to Rails. It  
can’t AFAIK read your mind (yet).

So, here we go, nothing special or extremely complicated:

map.connect “”, :controller =\> “item”, :action =\> “index”  
map.connect “items”, :controller =\> “item”, :action =\> “list”  
map.connect “item/:id”, :controller =\> “item”, :action =\> “show”

map.connect “member”, :controller =\> “member”, :action =\> “index” #  
You don’t really need this, this obeys the standard Rails convention  
map.connect “members”, :controller =\> “member”, :action =\> “list”

You might also want to take a look at named routes.

//jarkko

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 14, 2005, 11:28am UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/8 "2005-11-14T11:28:40Z")

</div>

On 14.11.2005, at 12.16, CSN wrote:

> Cool, thanks. Is there a URL which has documentation  
> for ‘named routes’? There doesn’t appear to be  
> anything map/route related at [api.rubyonrails.org](http://api.rubyonrails.org)  
> (figured it’d be ActionController::Routes or  
> something).

Sorry, forgot that. [Peak Obsession](http://wiki.rubyonrails.com/rails/pages/NamedRoutes)

> And in what release is mind-reading planned?

There is a debate going on on whether that would make Rails a bit too  
‘opinionated’. But we’ll see.

//jarkko

---

<div class="post-metadata">

**Author:** ![cool\_screen\_name900](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@cool\_screen\_name900](https://www.ruby-forum.com/u/cool_screen_name900)\
**Post date:** [November 14, 2005, 11:19am UTC](https://www.ruby-forum.com/t/possible-to-change-controllers-default-action/57046/9 "2005-11-14T11:19:38Z")

</div>

Cool, thanks. Is there a URL which has documentation  
for ‘named routes’? There doesn’t appear to be  
anything map/route related at [api.rubyonrails.org](http://api.rubyonrails.org)  
(figured it’d be ActionController::Routes or  
something).

And in what release is mind-reading planned?

thanks  
csn

— Jarkko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > [mysite.com/item/id](http://mysite.com/item/id) → item’s show action  
> > tried, but it became complicated (I didn’t know  
> > want that the  
> > “index”  
> > :action =\> “list”  
> > — Jarkko L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > However, you don’t want the action index to be  
> > > probably run into trouble for this exact reason,
> > > 
> > > > > If you look at scaffolded code, the second
> 
> –  
> Jarkko L.  
> [http://jlaine.net](http://jlaine.net)  
> [http://odesign.fi](http://odesign.fi)
> 
> > * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

craig

* * *

Yahoo! Mail - PC Magazine Editors’ Choice 2005

> **[\[object](https://mail.yahoo.com/)**
>
> \[object
