LondonGuy
LondonGuy

Reputation: 11098

How to display my root_url on a page as "example.com" rather than "http://example.com"

I'm trying to achieve this url on my users profile in a cleaner way: Profile Address: localhost:3000/username

This is how I do it: <%= link_to "localhost:3000#{root_path}#{@user.username}", @user.username %>

Quite an ugly way to do it. I could store the url I want in a variable in a helper or controller or something e.g. shortened_root_url = localhost:3000/ (or site name) and there are other ways I could do it ...

But isn't there a way I could just use root_url and have some magic remove the http:// from the url when displaying on users profile?

NOTE: I'm talking about the URL displayed to the user on his/her page, not in the href attr of an anchor tag.

Upvotes: 1

Views: 1002

Answers (1)

Taryn East
Taryn East

Reputation: 27747

What you're doing is unusual, really... so there probably isn't a direct way of doing it.

you can look into the "url_for" options to see what would be most applicable. http://apidock.com/rails/ActionView/Helpers/UrlHelper/url_for

It has options like "protocol" or "path_only" that might help you build what you need.

For your needs, if all you're doing is removing the protocol, why not just create the full url and use gsub eg:

link_to @user.username, username_url(:username => @user.username).gsub("http://", '')

You'd also need an appropriate route. I've just guessed at your routing here, but say in Rails 2.3.X you'd have something like:

map.username '/:username', :controller => :users, :action => :show

Upvotes: 2

Related Questions