Set non-conflicting toplevel path with show action in Rails 4

1 min read Original article ↗

I have and app with a users controller and I want to have it as a toplevel path in my routes, like:

get ':id' => 'users#show', as: :user_profile

and my to_param method in User is:

def to_param
  self.username
end

So that when you hit "/rodrigo" for example, it will look for the User object with the username = "rodrigo". So far, so good.

But I also have some static pages that I want to have toplevel paths as well, such as about, terms,

controller :home do
  get 'about',       to: :about,    as: 'about'
  get 'help',       to: :help,     as: 'help'
  get 'terms',      to: :terms,    as: 'terms'
  get 'privacy', to: :privacy,  as: 'privacy'
end

what happens is that when I try to access any of these static pages I get:

NoMethodError in Users#show
Showing /Users/rodrigovieira/Code/golaco/app/views/users/show.html.erb where line #1 raised:

 undefined method `name' for nil:NilClass

Also, my users#show routes is defined before the static pages routes in routes.rb.

that is, Rails thinks I'm talking about a user object. How can I circumvent this problem?

I'm pretty sure it's possible. I appreciate any help.