Forum: Ruby on Rails Using record variable in routes

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.
3efac2b493d393d9a8f00a90d4a051ed?d=identicon&s=25 David Lelong (drlelon)
on 2009-03-09 02:45
I have two models in my application.  One model is the project and it
has many images.  I'd like to configure my routes so the URLs show the
names of the projects and images instead of the IDs.

For example:

/projects/brooklyn-loft

would display detail about a specific project where "brooklyn-loft" is a
variable and unique for each project record,

and

/projects/brooklyn-loft/master-bathroom--2

would display detail about an image that belongs to the project and
"master-bathroom--2" is a variable and unique for each image record.

Any ideas on how I would setup my routes to dynamically generate like
this?

Thanks,

David
6fabb84e727dee98d1277df56458cbac?d=identicon&s=25 Andrew Bloom (Guest)
on 2009-03-09 05:49
(Received via mailing list)
# routes.rb
map.connect "/projects/:project_slug/:image_slug", :controller =>
"images", :action => "show"

# images_controller.rb
def show
  @project = Project.find_by_slug(params[:project_slug])
  @image = Image.find_by_slug(params[:image_slug])
  ...
end

On Mar 8, 8:45 pm, David Lelong <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.