removing 'controller_id' completely from URLs

Hi all,

Relative RoR newbie here. I’ve been researching routes, but I haven’t found what I need.

I have an app which has one primary controller (store) which provides a web UI (there are several other controllers for internal ‘stuff’). What I’m trying to do is completely remove the controller name from all generated URLs.

Here’s what I currently have in the routes file:

ActionController::Routing::Routes.draw do |map|

  ...

  map.connect '', :controller => 'store'

  map.connect ':controller/:action/:id'

  ...

end

In my templates, the following link_to

<%= link_to "Home", :action => "index" -%>

generates the following URL:

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

Any action other than ‘index’ seems to insert the controller name into the URL. For example, this link_to

<%= link_to "News", :action => "display_news" -%>

generates the following URL:

[http://localhost:3000/store/display_news](http://localhost:3000/store/display_news)

What I would like the previous link_to to generate is:

[http://localhost:3000/display_news](http://localhost:3000/display_news)

Any help is appreciated.

Carpe viam,

Mike

Michael Larocque

Chief Cook and Bottle Washer

Prolumina Communications Inc.

http://prolumina.com/~mlarocque/

Hi all,

Relative RoR newbie here. I’ve been researching routes, but I haven’t found what I need.

I have an app which has one primary controller (store) which provides a web UI (there are several other controllers for internal ‘stuff’). What I’m trying to do is completely remove the controller name from all generated URLs.

Here’s what I currently have in the routes file:

ActionController::Routing::Routes.draw do |map|

...

< /SPAN> map.connect ‘’, :controller => ‘store’

map.connect ':controller/:action/:id'
...

end

Hmm, not sure how that got munged. routes.rb looks like this:

ActionController::Routing::Routes.draw do |map|

map.connect ‘’, :controller => ‘store’

map.connect ‘:controller/:action/:id’

end

> What I would like the previous link_to to generate is:

> http://localhost:3000/display_news

Try adding the following to your routes.rb.. it should have the desired result.

map.connect '/display_news', :controller => 'store', :action => 'display_news'

Neil

But that requires updates to routes.rb whenever I add a method to the store controller. Not good.

Essentially, any time I generate a link to an action within the 'store' controller, I want the the URL to be of the form:

  http://example.com/action/whatever

and _not_

  http://example.com/store/action/whatever

There's got to be a simple way to do this.

Carpe viam, Mike

Michael Larocque Chief Cook and Bottle Washer Prolumina Communications Inc. http://prolumina.com/~mlarocque/

Don't have rails here to test this out on but I believe this would work:

map.root '/:action', :controller => 'store'

This I believe will capture all "http://server:port/:action" calls and route them to the StoreController and the proper action.

I usually name my routes for easy link generation, that's why it's map.root instead of map.connect.

Curtis

Thanks Curtis. That seems to have pointed me in the right direction. Currently, I have

ActionController::Routing::Routes.draw do |map|    map.connect 'account', :controller => 'account' # login    map.root '/:action/:id', :controller => 'store'    map.connect ':controller/:action/:id' end

and this seems to work.

Carpe viam, Mike

Michael Larocque Chief Cook and Bottle Washer Prolumina Communications Inc. http://prolumina.com/~mlarocque/

The order of the routes makes a big difference. Out of curiosity what happens currently if you go to a :controller/:action (leaving out the '/:id' portion?)

For example, if you have 'cart' controller and you try to access http://server:port/cart/index does it work? I'm wondering if the map.root rule will match it instead thinking that you mean 'cart' = ':action' and 'index' = ':id'. You should be fine as long as it's ':controller/:action/:id' since that won't match the map.root rule (cause it has 3 parts). Also what happens if you do http://server.port/cart (which I would cause you want to be the cart controller with the default action of index).

If that does happen you may want to move map.root to the bottom and specify a requirements on ':controller/:action/:id' such that it doesn't match your store controller.

map.connect ':controller/:action/:id, :requirements => { :controller => /(?!store)/ }

I believe that requirement regex will work (haven't tried zero-width negative look-ahead assertions in routes yet.

Just some things you might want to look into. Curtis

Thanks Curtis,

Yeah, I'm looking into this some more - after further testing I was seeing several problems. I'm looking into this now - thanks for the suggestions above.

Carpe viam, Mike

Michael Larocque Chief Cook and Bottle Washer Prolumina Communications Inc. http://prolumina.com/~mlarocque/

Thanks Curtis. That seems to have done the trick.

Carpe viam, Mike

Michael Larocque Chief Cook and Bottle Washer Prolumina Communications Inc. http://prolumina.com/~mlarocque/