Forum: Ruby on Rails Routing requests without ID parameter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0710cfc15c46974b3bec89df37ff68f4?d=identicon&s=25 Diego (Guest)
on 2006-05-01 20:24
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.
A0ed1bbfe42f4f87e6db0a16706246e2?d=identicon&s=25 Guest (Guest)
on 2006-05-02 23:02
> 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
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-05-02 23:30
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.
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-05-03 18:58
(Received via mailing list)
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 Bowman
www.recentrambles.com
This topic is locked and can not be replied to.