Xeroxoid
Xeroxoid

Reputation: 2155

Dynamically generated routes/URLs from Model fields in Rails 3

Describing the following scenario:

  1. A user signs up and provides a firstName (john) and a lastName (jagger)
  2. A route is automatically generated for the default domain i.e. www.asdasd.com/john.doe
  3. A guest visits www.asdasd.com/john.doe and is taken to the 'view' action of the controller for this user

Is something like this possible? I do not know how to form something like this in routes.rb

Thanks!

Upvotes: 1

Views: 654

Answers (2)

hs2d
hs2d

Reputation: 6199

Take a look at friendly_id. It doesnt generate routes dynamically but instead it lets u use the name as the ID.

Upvotes: 2

bor1s
bor1s

Reputation: 4113

Rails provides method in your models called to_param. This method returns URL for your model instance.
For example: you have model User
user = User.find_by_name('John')
user_path(user) # => /users/1

You can ovverride to_param method to return URL such as:
/users/John

Here you can read more:
http://apidock.com/rails/ActiveRecord/Base/to_param

Upvotes: 1

Related Questions