Search code examples
ruby-on-rails-3subdomain-fu

Mapping domain.com/user/1 to domain.com/loginName


Has anyone tried before using subdomain-fu to change domain.com/user/1 to domain.com/UserName ?

Are there any other plugins to make this happen on rails 3?


Solution

  • You can do this by "overwriting" the to_param method in your User model.

    Assuming you are using a User model, as you stated put some code like this in it:

      #cleans up the url so it's purdy.
      def to_param
        loginName # you probably want login_name or just name, but whatever it's your code
      end
    

    So notice that you now have to change your User#show controller call though, since you really are doing a

    User.find_by_name(params[:id] 

    Lastly, we should be able to change the routes so that the last route in your routes.rb file says:

    match ':id' => 'users#show'