Rails default image absolute URL - ruby-on-rails-3

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,

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.

Related

How to make seo url for Yii $_GET method using url manager?

I'm working on a site on local server. I have made a form to search country,state and city. After getting the results I see the URL formatted as URL
I want to make this URL as URL
So here I want to know about URL manager rules so I can make it as I want.
Simply add this rule in your url-manager
"site/searchme/<country>/<state>/<city>" => "site/searchme"
Now, you need to have an action with this signature:
public function actionSearchme($country, $state, $city)
You can access $country, $state, $city from url inside this action. For example if your url be like http://localhost/yii_1/site/searchme/UnitedStates/Washington/NewYork, $country will equal "UnitedStates" and so on.

Yii transform url from param/param/param/ to param/param?param

Suppose I have a url like:
site.com/param1/value1/param2/value2/param3/value3/param4/value4
I need to convert this url when a user writes it in url line to:
site.com/param1/value1/param2/value2?param3=value3&param4=value4
P.S. - the number of parameters is variable.
How can I do it?
You need to change the UrlManager Rules according to the situation.
You need to configure the Url manager to handle the first two params as path url and let the rest

How to urlize a link in Rails

I have a user defined external URL that I'd like to turn into a link by using something similar to Django's urlize filter. How might one go about doing that?
I just need something to add in the preceding http:// or whatever if it's lacking.
Unless I missed it, link_to doesn't seem to do that.
Here's a simple helper method to prepend an http prefix if needed:
def url_with_protocol(url)
/^http/.match(url) ? url : "http://#{url}"
end
> url_with_protocol("google.com")
=> "http://google.com"
> url_with_protocol("http://google.com")
=> "http://google.com"
> url_with_protocol("https://google.com")
=> "https://google.com"
I can see a couple of solutions:
create a helper urlize(url) that adds http:// if it's missing
override the url getter on your model to add the http://
add a before_save callback in your model to add the http:// to the url, thus making sure that you have a valid url in your db
Personally, I just have some validations that check that the url entered is valid. Here, I would use the 3rd option.

How to get images from Blizzard Community Platform

We can get some information by json.
But how can i get images by the information in json.
e.g. "thumbnail": "medivh/1/1-avatar.jpg"
It does not work when i concat the url behind the host + request.
So, is there some other way to get images?
The url for the static images has the following format:
http:// REGION . battle.net/static-render/ REGION / THUMBNAIL
Example:
For the avatar image
http://eu.battle.net/static-render/eu/alexstrasza/57/51685945-avatar.jpg
The picture you see when you visit your armory profile
http://eu.battle.net/static-render/eu/alexstrasza/57/51685945-profilemain.jpg
Another angle, I don't know where this is used
http://eu.battle.net/static-render/eu/alexstrasza/57/51685945-inset.jpg
As of the latest changes to the static renderer, the following is correct:
http://render-<Region>.worldofwarcraft.com/character/<Thumbnail>
For example:
http://render-us.worldofwarcraft.com/character/kul-tiras/148/130814612-profilemain.jpg

How to Detect and Redirect from URL with Anchor Using mod_rewrite/htaccess?

I've seen a number of examples of the opposite, but I'm looking to go from an anchor/hash URL to a non-anchor URL, like so:
From: http://old.swfaddress-site.com/#/page/name
To: http://new.html-site.com/page/name
None of the examples at http://karoshiethos.com/2008/07/25/handling-urlencoded-swfaddress-links-with-mod_rewrite/ have functioned for me. It sounds like REQUEST_URI has the /#/stuff in it, but neither me nor my Apache (2.0.54) see it.
Any ideas, past experiences or successes?
Anything after the # is a fragment, and will not be sent to the webserver. You cannot capture it at any point there, you'll have to use a client-sided approach to capture those.
#RobRuchte : would it not be better to use window.location.hash, with a replace instead of a regular expression?
var redirectFragment = window.location.hash.replace(/^#/,'');
if ( '' !== redirectFragment ) {
window.location = 'http://new.html-site.com' + redirectFragment;
}
I'm the author of the post you linked to. Wrikken is correct, the content after the named anchor is not sent to the server unless something has mangled the URL along the way. On the client side, you need some JavaScript like this in your landing page to redirect the swfaddress links to corresponding URLs on another domain:
var re = new RegExp('#(.*)');
var redirectFragment = re.exec(document.location.toString());
if (redirectFragment!=null)
{
document.location = 'http://new.html-site.com'+redirectFragment[1];
}
I used a modified version of the answer by #m14t. This works for redirects that look like http://example.com/path/to/page#fragment --> http://example.com/path/to/page/fragment. Notice that I also concatenated the window.location.pathname for the redirect, otherwise I would not get the full path for the redirect. If the new file path is completely different from the old one, then this would not work.
var redirectFragment = window.location.hash.replace(/#/,'/');
if ( '' !== redirectFragment ) {
window.location = 'http://example.com' + window.location.pathname + redirectFragment;
}
In my case, I needed to build fragmented links into individual pages, which is part of what is commonly done to improve a website's SEO.