Trouble understanding Routes

Hey Everyone,
I’m new to Rails and working through the book Simply Rails 2. I’ve
just hit one of my first large snags.

This was my routes file:

map.resources :stories, :has_many => :votes, :collection => { :bin
=> :get }
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
map.resource :session
map.root :controller => ‘stories’
map.resources :users

my problem was anytime I traveled to the url /users/1-username/ i got
an error saying the only action was ‘show’
Which did and did not make sense to me. It did make sense becuase the
only action in my controller was show. and in both theory (and I tried
it and it worked) the url should be /users/show/1-username. Which
worked without a problem. But of course the book only user the /users/
1-username/ url.

So after some time i changed my routes file to:

map.resources :users
map.resources :stories, :has_many => :votes, :collection => { :bin
=> :get }
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’
map.resource :session
map.root :controller => ‘stories’

Which fixed my problem. I can now access /users/1-username/ as
expected (displays user information).

But I really don’t understand why. Why is it now the controller is
defaulting to it’s only action but before it was not?

Hi brianp

 Your default routes should have the least priority Means

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

These should be at the very bottom of routes.rb

Sijo

map.resources

This is the same thing as stating that you have a RESTful controller
setup and that you have paths in place for all 7 controller methods
(index, show, new, edit, create, update, and destroy).

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

This allows you to have urls in place for…

http://yourdomain.com/controller/action/id OR
http://yourdomain.com/controller/id/action etc.

If you haven’t modified how your routing and paths are going to behave
in each of your controllers then you want to make sure those two lines
are always at the very bottom of the your routes file. Routes have
precedence in place for their order.

If you want to use javascript in your routes you would place something
like this:

map.js ‘:controller/:action.:format’

… at the very bottom as well.

If you wanted a catchall routes type of path set you can set the
following at the bottom:

map.connect “*anything”, :controller => ‘controllername’, :action =>
‘request_error’

… just an example …

You should check out:

http://guides.rubyonrails.org/routing.html

Sijo,

That is not necessarily good advice. You don’t always need (or want)
these default routes.

Even the comments produced automatically when creating a new Rails app
tell you this:

Install the default routes as the lowest priority.

…You should consider removing them or commenting them out if

you’re using named routes and resources.

E. Litwin wrote:

Sijo,

That is not necessarily good advice. You don’t always need (or want)
these default routes.

Even the comments produced automatically when creating a new Rails app
tell you this:

Install the default routes as the lowest priority.

…You should consider removing them or commenting them out if

you’re using named routes and resources.

Yes that is right. Sorry I forgot to tell that

Sijo

Thanks for the responses. I think I understand it now so I’ll recap
real quick

// Routes get written in a priority order. Specialized routes on the
top (in this case assuming a RESTful manner from the controllers)
map.resources :users
map.resource :session
map.resources :stories, :has_many => :votes, :collection => { :bin
=> :get }

// And the default layout for all routes not specifically set above
map.root :controller => “stories”
map.connect ‘:controller/:action/:id’
map.connect ‘:controller/:action/:id.:format’

Thanks again,
brianp

On Jul 31, 7:51 am, Alpha B. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs