Before i begin with my question, i would like to introduce myself. So i was introduced to ruby on rails via a friend around a couple of months back, when i decided to enroll myself with onemonth. Recently i was trying to create a profile page for the users based upon their username.
So the profile page should be http://www.example.com/john-doe
This is when i realized, what if a user decides to set their username as forum? I already got a controller named forum.
How can i let people have their own profile pages and at the same time avoid having them choose a name that already belongs to a controller.
Alright, so i had a look at friendly_id
and i guess this pretty much solves the problem for me. Even though while i am implementing friendly_id i would highly appreciate any kind of feedback that i can get from you people here.
So basically what you're gonna need is 2 things:
/forumin your config/routes.rb so that your forum matches first.
forumcouldn't be taken as a name. This isn't required although it's good practice. If you don't do it, a user could register as
forumbut they wouldn't be able to access their account. Usually this is someone trying to game the system so they'll have to reach out to you for support to change it. If you do make a blacklist, you'll have to keep this updated as you add more things areas into your site.
And as a fun fact, this is the reason why
resources :users generates
/users/:id so that you can avoid those namespacing issues. Obviously that's not always ideal if you want something like twitter or github's urls, but definitely cool to know.
I had to chime in on this too. I agree on blacklisting the word forum. It's an easy fix and something I just learned about after implementing
You'll want to open
config/initializers/friendly_id.rb and add your namespace to the array shown below:
This should take care of the issue for you. But also make sure to place your route in the proper order as Chris mentioned.
Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.