# Using record variable in routes

**URL:** <https://www.ruby-forum.com/t/using-record-variable-in-routes/162346>\
**Category:** Rails\
**Created:** [March 9, 2009, 2:45am UTC](https://www.ruby-forum.com/t/using-record-variable-in-routes/162346 "2009-03-09T02:45:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [March 9, 2009, 2:45am UTC](https://www.ruby-forum.com/t/using-record-variable-in-routes/162346/1 "2009-03-09T02:45:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [March 9, 2009, 5:49am UTC](https://www.ruby-forum.com/t/using-record-variable-in-routes/162346/2 "2009-03-09T05:49:01Z")

</div>

# 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 L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
