Search code examples
ruby-on-railsdatabaserails-routing

Passing parameters as path variables in ruby on rails


I'm still new to ROR, so pardon the simplicity of the question...

So http://www.example.com/controller/:id displays a record in my table, with :id being a number (1,2,3 etc.).

Is there a way I can have :id in the URL be the value of a field in the displayed record? Such that I can have http://www.example.com/controller/record_field? I want to have a human-friendly reference to specific records in my table. I'm sure this must be possible. Do I change something in routes.rb?

Thanks for the help!


Solution

  • The cleanest way is to add a new find method in your model (or simply use the find_by_fieldname Rails gives you in your control). Then you'll have your controller use that method instead of the regular find(params[:id]) to pull your model record.

    Check out Ryan B's screencast on this here. It's pretty easy, and he's a good teacher, so you shouldn't have any problems.