get the seo-unfriendly version of a url - ruby-on-rails

I'm adding disqus commenting to some articles on our site and all URLs are SEO friendly.
This means that, if the title of the article changes so will the URL of that article, which will discard the previous disqus comments (linked to the previous version of the URL).
The solution would be to strip out the title of the article from the URL before passing it to Disqus.
So I need to turn "http://mydomain.com/article/123-myarticle/section/1-sectiontitle" into "http://mydomain.com/article/123/section/1"
What is the easiest way to do this?
Thanks!
PS: I'm very new to Rails (i'm taking over a developed project)

You don't need to extract anything from the URL.
All you need to give to Disqus is a unique id.
So you can add a method to your model, called disqus_id for instance:
def disqus_id
"name_of_your_model_#{id}"
end
and then, in the javascript:
disqus_identifier = "<%= #your_model.disqus_id %>";

Related

URL redirection JSF

I have my my app url: 127.0.0.1:8080/reader/read.xhtml
The read.xhtml is populated thorough database and has got various
links which are hard coded in database. (read.xhtml is actually
retrieved as String from DB.) for e.g. there are links
(<a href ="/write.xhtml>write</a>)
/write.xhtml
/upload.xhtml
as I cannot add the context when i click the link it directs me to
WWW://127.0.0.1:8080/write.xhtml or
HTTP://127.0.0.1:8080/upload.xhtml
Is there any way I can redirect the link to
HTTP://127.0.0.1:8080/reader/write.xhtml.
Can Prettyfaces handle this. If yes how?
You can simply add the context path to the links by rendering it in front of your links. Something like:
write

A shorthand for multiple links?

I have a fairly long list of quick links (approx. 20) that I'm going to embed on my main page. Instead of adding the full url of each link to the each anchor, is there a way to add maybe the end of each link.
For instance, if the link to the url is
http://support.proboards.com/index.cgi?display&thread=423890
as you can see, my forum is a proboards forum if that helps anything here.
the location of each url is local,
Instead of adding the complete url, is there a way to use something like
<a href = "&thread=423890">
or maybe
<a href = "this.thread=423890"> ?
Not in pure HTML, the best you'll get is <a href="index.cgi?display&thread=423890">, which is what you should already be using. Of course I'm assuming support.proboards.com is your site here.
You could use JavaScript and call a function like openThread(423890) if you really wanted, but a)I wonder what you really gain from it, b)this won't work for people with JavaScript turned off (a la NoScript), and c)I wonder if Google would properly see all the links.
Presumably, these pages are all being generated by server-side script anyway, so you should only really be coding index.cgi?display&thread= once anyway...
If you're hard-set on doing something shorter, I'd suggest using URL rewriting. You can have a URL like .../display/thread/423890 and then use something shorter if you're already from a /display/thread/ page. But this requires the link to go to a page you have control over.

How to retrieve web site favicons?

I am using Ruby on Rails v3.0.9 and I would like to retrieve the favicon.ico image of each web site for which I set a link.
That is, if in my application I set the http://www.facebook.com/ URL I would like to retrieve the Facebook' icon and use\insert that in my web pages. Of course I would like to do that also for all other web sites.
How can I retrieve favicon.ico icons from web sites in an "automatic" way (with "automatic" I mean to search for a favicon in a web site and get the link to it - I think no because not all web sites have a favicon named exactly 'favicon.ico'. I would like to recognize that in an "automatic" way)?
P.S.: What I would like to make is something like Facebook makes when to add a link\URL in your Facebook page: it recognizes the related web site logo and then appends that to the link\URL.
http://getfavicon.appspot.com/ works great for fetching favicons. Just give it the url for the site and you'll get the favicon back:
http://g.etfv.co/http://www.google.com
Recently I have written some similar solution.
If we want find favicon url, that can be not only .ico file and can be not in the root, we should parse target site html.
In Ruby on Rails, I have used nokogiri gem for html parsing.
First we parse all meta tags where itemprop attribute contains image keyword. It is necessary in situations where target site used https://schema.org/WebPage template, that more modern technology than just link tag.
If we found it, we can use content attribute as favicon url. But we should check it for really URL existence, just to be sure.
If we can't found some meta tags, then we search for standard link tags, where rel attribute contains icon keyword. This is W3C standard situation (https://www.w3.org/2005/10/howto-favicon)
And some code of my solution:
require 'open-uri'
def site_icon_link site
icon_link = nil
url = nil
doc = Nokogiri::HTML(open(site))
metas = doc.css("meta[itemprop*=image]")
if metas.any?
url = metas.first.attributes['content'].value
else
links = doc.css("link[rel*=icon]")
if links.any?
url = links.first.attributes['href'].value
end
end
if url =~ URI::regexp
icon_link = url
elsif (site + url) =~ URI::regexp
icon_link = site + url
end
icon_link
end
The favicons are being found by two ways. First, there is a 'hardcoded', traditional name of `http://example.com/favicon.ico'.
Second, the HTML pages may define the favicon in their <head> sections, by <link rel="icon"...> and a few other. (You may want to read the Wikipedia article about favicon)
So, your automat may fetch the main page of given website, parse it and check whether there are proper <link> tags, and then, as a fallback, try the "hardcoded" favicon.ico name.
I think I missed your question ...
you want to grab a favicon from another site and make it yours?
if that's what you want, you can get directly from the home icon and save it in your public folder.
thus: www.facebook.com favicon: www.facebook.com/favicon.ico
take that image and save with the name favicon in your public folder
done it should be sufficient
if you want it dinamicaly you can use jquery, but if you want that static you can put a image tag pointing to: [root url of the website]/favicon.ico
like this: <%= image_tag "#{website.url}/favicon.ico" %>
With javascript (jQuery), like this: http://jsfiddle.net/aX8T4/
Can't you just use a regular img tag with the src attribute pointing to the favicon?
<img src="http://www.facebook.com/favicon.icon">
This assumes a browser recognizes a .ico file as an image. Helped methods would probably work with this too.
You can do it easily with pismo gem.
Quick example to get the url of Facebook's favicon:
Pismo::Document.new('http://www.facebook.com/').favicon
Here's my ruby method, that will strip the end off a URL, append the favicon, and produce an image tag.
def favicon_for(url)
matches = url.match(/[^:\/]\/(.*)/)
image_tag url.sub(matches[1], '') + '/favicon.ico', {width: '16px', height: '16px'}
end

Rails 3.1 build GET form that creates a custom URL route that is SEO friendly

I would like to create custom SEO friendly routes similar to what is used by http://realestate.com.au For example the following page is shown by google when the search term "real estate melbourne" is used:
www.realestate.com.au/buy/in-melbourne,+vic+3000/list-1
I would like use the following format. mysite.com/trips/search/melbourne-to-sydney/01-01-2011
I have configured the routes in my routes.rb file to get it to pick up the correct parameters when a url is entered is this format.
routes.rb
match '/trips/search(/:fl(-to-:tl(/:tripdate)))' => 'trips#someaction'
My question is how do I setup a form in rails 3 to send a GET request using the above url structure. I have tried playing around with to_params though it seems to then change all my edit, show links etc which is not intended. I could build the link using javascript though I guess this would be a hacky option and the site would not work if javascript was disabled.
Is there a neat way to be able to create a GET submit form in Rails 3.1? The fields are select lists containing name and ids.
Thanks for your help.
This will help you immensely with the friendly URL portion
http://norman.github.com/friendly_id/file.Guide.html
https://github.com/norman/friendly_id

Rails - Given a block of text, auto-link links

on mysite I have the ability to add comments. Sometimes users enter comments will links (href. ...)
I would like to have those links to be clickable/linkable (a href) when the comment is displayed to users.
how with Rails 3 can I take a comment, and look for links and then wrap those links in an a href tag that opens in a new window?
Thanks
The simplest way is to use the auto_link method built into rails.
Note: In Rails 3.1 auto_link has been moved into a separate gem.
idlefinger's suggestion of #auto_link is perfect. I know it's not the question you originally posed, but wanted to suggest: also check out #simple_format, which will nicely format your users' use of newlines into br and p tags.

Resources