Connor
Connor

Reputation: 4168

Rails URL question

If I have a Users model, and then an individual user, say, person1, is there a way to have the url: www.myapp.com/person1 show what www.myapp.com/users/person1 would? What would that be called?

Thank you!

Upvotes: 2

Views: 52

Answers (2)

Fabio
Fabio

Reputation: 19176

You should define a route in your routes.rb file:

match "/:user" => "users#show"

and you'll get the username given in params[:user]. But you need to know that this kind of route could override other routes defined, because it will match any string, so you should at least define some constraints on the username.

For example if your usernames matches a regexp you could define it as a constraint

match "/:user" => "users#show", :constraints => { :user => /some-regexp/ }

and don't forget to set this route as the last one in your routes file, otherwise it will clash for sure with other routes.

Read this for full reference

Upvotes: 4

rogerdpack
rogerdpack

Reputation: 66751

add "two" routes for the same thing to your app.

 # Shorter url to show store items by tags
map.connect '/s/tags',
  :controller => 'music',
  :action     => 'show_by_tags'
map.connect '/music/s/tags',
  :controller => 'music',
  :action     => 'show_by_tags'

Upvotes: 0

Related Questions