Routing requests without ID parameter


#1

Hi there,

This seems to be obvious but after reading a few times the section in
the Agile Wb Dev with Rails I’m still strugling.

I have a table with categories names which I want to display in the url
instead of their id. So far I managed to get both in the url by adding:

map.connect ‘/:id/:category/’, :controller => “site”, :action => “show”

in the routes.rb file and calling the link in the view as below:

<%= link_to category.name, :action => ‘show’, :id => category, :category
=> category.name %>

I tried to take the id out of map.connect but then I get the error
message below as the controller is looking by id’s and not the category
name:

“Couldn’t find Category without an ID”

Should I modify the controller to look by the name parameter instead of
the id or am I missing something in the routes.rb file?

Anyone there who can help out?

Thanks a lot.


#2

Should I modify the controller to look by the name parameter instead of
the id or am I missing something in the routes.rb file?

Anyone there who can help out?

Thanks a lot.

If you want only the category in the url…

map.connect ‘:category/’, :controller => “site”, :action => “show”

Then in you’re controller use

Model.find_by_category


#3

What actions besides /site/show do you want to do this on? If you want
to show an individual item, you probably want /site/show_item/33 to
work, right?

Then for this one action you’d want something like

map.connect ‘/site/show/:category’, :controller => “site”, :action =>
“show”

or since you probably have a couple of actions that can work like this
but a lot of others that need a numeric ID, maybe have one controller
where all the actions use the :category string as the identifier, in
which case you could say

map.connect ‘/site/:action/:category’, :controller => “site”

and in your show method in the controller you can’t use
Foobar.find(params[:id]). Instead you’d need something like

Foobar.find_by_category(params[:category])

Diego wrote:

Hi there,

This seems to be obvious but after reading a few times the section in
the Agile Wb Dev with Rails I’m still strugling.

I have a table with categories names which I want to display in the url
instead of their id. So far I managed to get both in the url by adding:

map.connect ‘/:id/:category/’, :controller => “site”, :action => “show”

in the routes.rb file and calling the link in the view as below:

<%= link_to category.name, :action => ‘show’, :id => category, :category
=> category.name %>

I tried to take the id out of map.connect but then I get the error
message below as the controller is looking by id’s and not the category
name:

“Couldn’t find Category without an ID”

Should I modify the controller to look by the name parameter instead of
the id or am I missing something in the routes.rb file?

Anyone there who can help out?

Thanks a lot.


#4

yes, your controller can only see what you’ve passed to it in the url or
a post. try using find_by_name in your controller.

Charlie B.
www.recentrambles.com