Rails default image absolute URL

I have a newbie question:

I'm storing a default URL (pointing to an image that's stored on server) as the default image for a model, such as /assets/project_default.png. The user can also set the URL to a remote URL as well.

How do I display the URL as an absolute URL so it's http://xxxxxx.com//assets/project_default.png but also support a user's remote URL?

If I use "#{request.protocol}#{request.host_with_port}, that'll always apply. I'd like to apply only if the URL isn't remote.

Cheers,

Answers


Assuming I'm reading your question right, you could do something like...

application_helper.rb

def absolute_image_url (url)
  if url[0] == "/"
    return "#{request.protocol}#{request.host_with_port}" + url
  else
    return url
  end
end

Or add it into the model, etc.


Need Your Help

SQL to C# conversion

c# sql compiler-construction datatable where-clause

I have a working process, behind a web application, that is generating some records in a table, it is so general, that I actually had to add SQL (where clause) as Data in a table.

plus operator with quotes in javascript

javascript

Recently I saw a function in which arguments are surrounding with plus operator and quotes like this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.