Ask A Question

Notifications

You’re not receiving notifications from this thread.

Add Profile page for users

Rahul Lakhaney asked in Rails

Hello,

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.

Reply

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.

Reply

Hey Rahul,

So basically what you're gonna need is 2 things:

  1. Make sure this route is AFTER /forum in your config/routes.rb so that your forum matches first.
  2. You can add a blacklist into validations so that forum couldn'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 forum but 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.

Reply

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 friendly_id.

You'll want to open config/initializers/friendly_id.rb and add your namespace to the array shown below:

  config.reserved_words = %w(new edit index session login logout users admin
    stylesheets assets javascripts images forum)

This should take care of the issue for you. But also make sure to place your route in the proper order as Chris mentioned.

Cheers!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.