[raleigh.rb] Custom routes and ugly code

Geoff Davis geoff at geoffdavis.net
Sun Oct 29 07:45:13 EST 2006


D'oh.  I guess it would have been better to have actually read your
email.

A couple of simple things you can do:

1) Create a helper method for generating your company urls, e.g.

<a href="<%= company_url company %>"><%= company.name %></a>

If you have a lot of company links, you could add a second helper
method, e.g. link_to_company, that would be equivalent to the above:

<%= link_to_company company %>

2) redirect_to can take an absolute url as an argument instead of a set
of options, so if you make your helper method available to your
controllers (use the "helper" method), you can use your url method in
your redirects like so:

redirect_to company_url(company)


On Sat, 2006-10-28 at 18:14 -0400, Geoff Davis wrote:
> Named routes are your friend:
> http://wiki.rubyonrails.org/rails/pages/NamedRoutes
> 
> On Sat, 2006-10-28 at 16:58 -0400, Tony Spencer wrote:
> > I wanted to take advantage of Rails named routes and so in my first attempt
> > I¹ve attempted to change URL¹s for companies from
> > 
> > /company/show/1
> > 
> > to
> > 
> > /company/1/red-hat
> > 
> > So I added this to routes.rb:
> > 
> > map.companyshow 'company/:id/:name', :controller => 'company', :action =>
> > 'show' 
> > 
> > And then had to following in my views:
> > 
> > <a href="<%= companyshow_url(:id => company.id, :name =>
> > company.name.downcase.gsub(/ /, '-')) %>">
> > 
> > Yuck.  Is there a better way to do this?  Also obviously now my controller
> > redirects won't work with the usual:
> > redirect_to :controller => 'company', :action => 'show', :id =>
> > @product.company_id
> > 
> > How should I redirect to my named route properly?
> > 
> > 
> > _______________________________________________
> > raleigh-rb-members mailing list
> > raleigh-rb-members at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/raleigh-rb-members
> > 
> 
> _______________________________________________
> raleigh-rb-members mailing list
> raleigh-rb-members at rubyforge.org
> http://rubyforge.org/mailman/listinfo/raleigh-rb-members



More information about the raleigh-rb-members mailing list