Showing posts with label url. Show all posts
Showing posts with label url. Show all posts

Tuesday, September 25, 2007

helper: Getting the full URL

There’s nothing built in Rails that gives you the full URL. I made a helper, that gives this to you. It doesn’t display properly here.


def get_full_url(base_url)
return base_url + request.path + get_params_as_url_friendly
end

def get_params_as_url_friendly
count = 0
param_size = request.query_parameters.size
parameters = ""

for param in request.query_parameters
parameter = "#{param[0]}=#{param[1]}"

if count == 0
parameter = "?#{parameter}"
else
parameter = "&#{parameter}"
end

count += 1
parameters += parameter
end

return parameters
end

Helper: get_url(url)

Answer to making our relative URLs to absolute URLs through the application helper:


def get_url(url)
return "http://localhost:3000" + url
end


If URL were /model/file/1/a.jpg, then you'd get http://localhost:3000/model/file/1/a.jpg in return. When moving to a new webserver, we just need to edit the helper and change the base URL.