Where should I create an abbreviated URL on my users profile page for MVC architecture?

I have a Rails application where people have a profile page - http://prettylongdomainname.com/profile_username

To create a profile username, I use the before_create AR hook in my model:

before_create :generate_username

def generate_username
    self.username = a_user_name_i_generated
end

I would also like to keep the shortened URL in my user profile so that when they exchange things, I can automatically link to their profile page. I decided to use the bit.ly API to shorten the URL, but I'm not quite sure where I should put the code.

It makes sense that I have to save the shortened URL when creating the user, especially immediately after creating the profile_username username. However, I need to make an HTTP request in the bit.ly API in order to get the shortened URL.

Does anyone know a better way to do this?

Thank!

+3
source share
2 answers

I think you have the right idea using a callback, but instead I create an observer so as not to pollute the User class with external API calls. Here is more information about observers in rails .

+3
source

, user_username , . . (: ) .

API URL- . URL- .

+1

Source: https://habr.com/ru/post/1733378/


All Articles