Question

I'm pretty new at rails, so forgive me if I'm overlooking simple things or the rails way. My objective is to completely replace URLs of the form

/users/1

with

/username

for all purposes. (I think exposing IDs scaffolding publicly is like walking around with a bone sticking out of your arm.) But implementing seems a little more complicated than I expected. This seems to change the entire way rails indexes and looks up users, rather than just substituting a lookup method.

Although I've kind of gotten this to function using the to_param override in my user.rb file, I've read this means I'll have indexing problems down the road when using params([:username]), and I'm not sure how it will impact my

(a) session model at new user creation, and

(b) @User usage in the user/show.html.erb file.

So, I've either consulted the following pages (or asked the questions):

The major issues I'd like to understand from this question:

  1. What functionality do I lose by transitioning to this? That is, what things currently "just work" in rails that I'll have to address and rewrite if I pursue this replacement?
  2. As a practice, is this something better to replace with friendly_id? My concern here is that creating a slug column in my DB identical to the username seems a little non-DRY and makes me uncomfortable, and I'd rather avoid dependencies on external gems where possible.
  3. What does my users#show need to look like?
Was it helpful?

Solution 3

After working around this for a couple weeks, I'd say the best answer as of Aug 2, 2012 is that if you do this, you violate many rails conventions and rip apart the very fabric of time and space itself.

Ugly scaffolding in the URLs is a necessary part of rails' approach to RESTfulness.

OTHER TIPS

You should check out Friendly ID. Makes doing what you're trying to do incredibly easy.

https://github.com/norman/friendly_id

There's a Railscast for it, too.

http://railscasts.com/episodes/314-pretty-urls-with-friendlyid?view=asciicast

If your username contains a special characters like @, -, . and got an error that says "No route matches" then you need to filter its route. See below:

match "/user/:username" => 'users#show', :as => :profile, :username => /[\.a-zA-Z0-9_@-]+/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top