Reverse formatting routes...?

i have this set up
map.connect ‘startrekvoyager/episodes’, :controller =>
‘categories’, :action => ‘show’, :id => 2

but when i call
link_to “Voyager”, :controller => :categories, :action => :show, :id
=> 2

it comes up as
http://brocoum.com/voter/categories/show/2

i want it to come up as
http://brocoum.com/voter/startrekvoyager/episodes

just so it’s more readable

is there a way to do this “reverse” route lookup?

Thanks!

On Feb 22, 2008, at 2:11 AM, Stedwick wrote:

i want it to come up as
http://brocoum.com/voter/startrekvoyager/episodes

just so it’s more readable

is there a way to do this “reverse” route lookup?

Thanks!

Try removing the default route or placing it after the route you wish
to have precedence.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Arranging it hasnt worked, and i cant get rid of the defaults because
ill need them for other things. Heres my routes:

map.home ‘’, :controller => ‘categories’

map.connect ‘startrekvoyager/episodes’, :controller =>
‘categories’, :action => ‘show’, :id => 2
map.connect ‘startrekdeepspacenine/episodes’, :controller =>
‘categories’, :action => ‘show’, :id => 4

map.connect ‘startrekvoyager/characters’, :controller =>
‘categories’, :action => ‘show’, :id => 5
map.connect ‘startrekdeepspacenine/characters’, :controller =>
‘categories’, :action => ‘show’, :id => 6

Allow downloading Web Service WSDL as a file with an extension

instead of a file named ‘wsdl’

map.connect ‘:controller/service.wsdl’, :action => ‘wsdl’

Install the default route as the lowest priority.

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

On Feb 22, 2008, at 11:55 AM, Stedwick wrote:

Arranging it hasnt worked, and i cant get rid of the defaults because
ill need them for other things. Heres my routes:

Well, you can eliminate the need for default routes :wink:

Seriously, use map.resources or

with_options :controller => ‘some’ do |s|
s.bar ‘foo/bar’, :action => ‘show’, :id => 1
s.baz ‘foo/baz’, :action => ‘show’, :id => 2
end

and make sure there’s only one possible match for a given set of
parameters.

If you use named routes rather than map.connect everywhere, you can:

map.stv_episodes(‘startrekvoyager/episodes’,
:controller => ‘categories’,
:action => ‘show’, :id => 2)

link_to “Star Trek Voyager: Episodes”, stv_episodes_path

rather than specifying the id (which ties you to the particulars of
the data that I would personally avoid, but at least the change would
be limited to your routes file).

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Two basic options. First you can create a named route:

map.<route_name> ‘route’, :controller=>…

That way you can build your link_to’s, etc using <route_name>_url and
<route_name>_path. You’ll probably have to ditch the forward slash in
favor or a dash, however.

Second, you can override the ‘to_param’ method of ActiveRecord::Base.
By default it returns the id of the record but you can tell it to
return anything… including text. With that in mind I’d suggest
something like this:

class TelevisionSeries
has_many :episodes
has_many :characters

def to_param
name.gsub(/\s+/, ‘_’).dasherize
end


end

With that you can use the default RESTfully named routes if you also
nest the routes like so:

map.resources :television_series |tv|
tv.has_many :episodes
tv.has_many :characters
end

Which will result in routes like this:

http://localhost:3000/television_series/deep-space-nine/episodes
http://localhost:3000/television_series/deep-space-nine/characters

The last thing to remember is that you’ll receive the dasherized name
of the series in params[:id] so you’ll have to operate on it before
attempting to find… and you’ll use find_by_name.

On Feb 22, 2008, at 12:50 PM, Stedwick wrote:

The entire backend is simply an acts_as_tree Category model and an
Item model. Each category has subcategories, and the subcategories
have items. Who knows, I might want to vote on fruit one day :slight_smile:

Anyway, a named route like st_voy_url will never work because I will
never manually link to it. My index page simply iterates over all
categories with items and spits out an unordered list.

Does it make sense what I’m saying and what I’m trying to do?

Well, I don’t think you’ll ever be able to get URLs to look as nice
that way, but I see what you mean. Have you considered overriding
#to_param in your model to be something like a permalink? At least
then your URL would be something like:

http://www.brocoum.com/voter/categories/show/2-star-trek-voyager-episodes

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

The reason I can’t use named routes is because I’m writing my “index”
page.

You can see it here.
http://www.brocoum.com/voter/

Also, everything is stored generically in my database. I wanted it to
be as flexible as possible. In other words, not everything is going to
be Star Trek, or a TV show, or have characters and episodes.

The entire backend is simply an acts_as_tree Category model and an
Item model. Each category has subcategories, and the subcategories
have items. Who knows, I might want to vote on fruit one day :slight_smile:

Anyway, a named route like st_voy_url will never work because I will
never manually link to it. My index page simply iterates over all
categories with items and spits out an unordered list.

Does it make sense what I’m saying and what I’m trying to do?

[email protected]
whoa, that link URL actually works :slight_smile: I guess you can put anything
you want after the number.

thanks for all the help you guys, I’m not sure what I’ll do, but it’s
not really a big deal anyways. I might just use named routes and
manually link them.