Human readable URL causes a problem in Ruby on Rails

Posted by TK on Stack Overflow See other posts from Stack Overflow or by TK
Published on 2010-06-18T11:03:01Z Indexed on 2010/06/18 12:13 UTC
Read the original article Hit count: 177

Filed under:
|
|

I have a basic CRUD with "Company" model. To make the company name show up, I did

def to_param
  name.parameterize
end

Then I accessed http://localhost:3000/companies/american-express which runs show action in the companies controller.

Obviously this doesn't work because the show method is as following:

def show
  @company = Company.find_by_id(params[:id])
end

The params[:id] is american-express. This string is not stored anywhere.

Do I need to store the short string (i.e., "american-express") in the database when I save the record? Or is there any way to retrieve the company data without saving the string in the database?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about url