In my Rails app I have a user model (Devise) with url_field for Twitter.
Right now I have this in my show page:
<%= link_to @deal.user.company_twitter, @deal.user.company_twitter, :class => "comp_twitter" %>
It shows the entire link in the show page (even http://), that’s because my url_field makes the user add “http://” or it won’t validate the link.
I want the user to only add their “@name” instead of the whole Twitter link when creating their profile. I would also want to show only the @name instead of the Twitter link in the “show” page. How can I do this?
Simply don’t use an
url_field, but a regularstringdatabase field, liketwitter_username. When signing up, let the user enter their username, e.g.foo.For getting the real URL to the Twitter account, create a new method in your
user.rbmodel:The advantage is that at this point, in your model, you can include a custom validation that would check if this URL really exists after the user has submitted their
twitter_username.Finally, use that in your view instead of just the URL:
This would render: