2

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!

1
  • Check http://guides.rubyonrails.org/routing.html Also, your implementation will be a bit ambiguous, for example, what if user has a username users? or username that is equal to one of model names in your application? Commented Jul 20, 2011 at 22:44

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

awesome. Thank you, Fabio! I'll acccept it once I'm able to.
@ConnorMontgomery: I added some more details in the answer. P.S. You can always accept an answer, even with 1 rep ;-)
Yeah I know - it just says I needed to wait 7 minutes... 1 minute now. Once that time delta is done, I'll accept it ;) And thank you so much for the clarification!
0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.