Yearly Archive » 2014

Why Google did not Crawl our New Pages? – format order matters in respond_to blocks !!

Hello All,

So we bumped into a very interesting problem since last 2 Weeks. We rolled out our Spanky new Festival pages which we thought would allow our users to quickly grab best deals, coupons and forums discussion for all the Festivals that happen in India, the entire year around. Right from GOSF to Diwali to smaller festivals like Children Day Sale.

http://www.desidime.com/festivals

http://www.desidime.com/festivals/gosf

We thought it was cool, Our users liked it too But Google decided to remove that page from Index. Our SEO expert Suhan Shukla did everything he can do to get that page into Google’s index. But google decided to ignore it.

Today, Suhan found a solution for the same… He came to a logical conclusion on why Google decided to ignore the page. It turned out that Google Bots were seeing the page much differently than what we were seeing it on Desktop.

The “Aah” Moment:

Thats some Javascript code… There is definitely a hell lot of content on that page… So why does google not see it?

This CURL command gave more hints:


curl -D - -s -A 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' http://www.desidime.com/festivals/diwali

 

When running the same CURL command on localhost,  the problem became clearer.

class FestivalsController < ApplicationController
  def show
    @festival = Festival.find(params[:id)]
    respond_to do |format|
      format.js { render "tags/show"}
      format.html
    end
  end
end

If you look closely at the respond_to block, the format,js appears before format.html. Since bots Crawl pages without a particular format, Only the js gets rendered.

The Correct Way:

class FestivalsController < ApplicationController
  def show
    @festival = Festival.find(params[:id)]
    respond_to do |format|
      format.html
      format.js { render "tags/show"}
    end
  end
end

And yeah, its not yet fixed in Rails 4, So be careful and always give html the first priority so everyone on the web (including the bots) can read your pages!!

BIG Thanks to Suhan for digging and finding the problem using Google Webmasters “Fetch as Google” tool.

[Rails] ActiveRecord Time Zone – Not Automatically Set in Rails…

I bumped across this very interesting problem related to ActiveRecord time Zone…

I had some code snippet like this:

 query = "INSERT INTO link_logs VALUES(DEFAULT,'#{self.url}','#{self.affiliatized_url}','#{self.publisher_id}',DEFAULT,DEFAULT)"
ActiveRecord::Base.connection.execute(query);

Basically, a very simply Insert query to push url, affiliate_url , publisher_id and timestamps….

Problem: Timestamps were getting inserted in UTC Time Zone…

Solution:

First, I checked Application Time Zone in config/application.rb and it was correctly set to Mumbai.

config.time_zone = 'Mumbai'

I then used ActiveRecord to insert records and Voila, it obeys the time zone set in application.rb

However, When done via execute query, It defaults to UTC TimeZone… Here’s what even the console says:

irb(main):001:0> Rails::application.config.time_zone

=> "Mumbai"

irb(main):002:0> Rails::application.config.active_record.default_timezone

=> nil

Turned out,  an open bug already exists for the same:

https://github.com/rails/rails/issues/3145

A workaround appears to be adding the following line to application.rb:

config.active_record.default_timezone = :local

Strongly would suggest from my experience, to avoid surprises, Active Record Default timezone must be manually set for any rails app

[jQuery] How to remove text between two divs

While working on one project, I came to weird problem:

how to get rid of text that is between two div tags and is not captured in any tags?

To be specific, here is what I mean:

<div class="first">Some content</div>
Here is our text we wanna get rid of. 
<div class="second">Some other content</div>

The problem here is, that text is not surrounded by any HTML tag. Then it would be too easy. jQuery can easy hide() or remove() content.

So how do we go about it?

Well, I googled and tried a lot of solutions (eg. trying to put ending p tag before second, and starting p tag after first div) but that didn’t seems to help. So I gave up with trying to enclose text and then remove it. Finally, I came to this code:

jQuery('.first')[0].nextSibling.nodeValue = '';

nextSibling property of DOM and does exactly what you’d expect. So then we can easy set it’s nodeValue to empty string. That’s what we wanted right?

Here is a working example.

But what if there is more of those things?

Yes, that’s exactly what I was facing. My code is more like this, in fact:

<div class="first">Some content</div>
Here is our text we wanna get rid of. 
<div class="second">Some other content</div>
<div class="first">Some content</div>
Here is more text we wanna get rid of. 
<div class="second">Some other content</div>

Here’s where the 0 from jQuery code comes in role. In fact, before we expected to have just one first div. Now we have more divs, so let’s make sure all of them got noticed.

for (i = 0; i < jQuery('.first').length; i++) {
        jQuery('.first')[i].nextSibling.nodeValue = '';
    }

Here is working example.

Is that it?

Well, yes, if the only thing you have between two divs is always only text without any more HTML tags. If you have tags there, you will need to do some extra work.

And yes, you could use remove() on nextSibling as well.

A New Dawn

Unfinished stories leave behind a lingering mystery which she liked.

There were trouble-free days which  got her through. Feeling content with the country she was living in. She walked the cobbled street alone heading anywhere but home; breathing the polluted air the city offered her; sat in front of the laptop and powered through hundred mails every day; went to bed feeling exhausted but somewhere secretly satisfied with what life was showing her.

Then there are those days where she finds a familiar stranger knocking at her doors, which weaves into a dream of past which she finds herself confronting into.  It stares at her with an uncanny resemblance which sends a tingling shiver down her spine and reminds her why she still carries the scar on her back. It gently scrapes the healing wound and reminds her about the lug of emotions she carries which the mundane life distracts her from.

She looks back in her dream meekly afraid of hearing the truth. She wakes up with beads of perspiration on her fore-head. The sorrow sits like a rock on her heart. It is real and no longer a dream. She feels sick no matter how much she thinks it never happened.

A sudden comforting realization dawns upon her, she has the whole day to help her carrying this weight. A week of days. A month of weeks. A year of 365 days. In the fear of this familiar stranger of sadness she realizes, she had just wasted a whole day feeling discontent in her daily life carrying the excess luggage from her past.

It happened to her tonight. It may re-occur tomorrow. For sure as hell it has happened to her many yesterdays ago.

Why do we give the past more importance than it deserves. When are we going to learn that no one owes us anything? The past is like an ill-tempered child who is hungry but refuses to eat. It will heavily tug at your heart strings just as you remove your sleeping slippers and tuck yourself in bed, demanding attention, knocking on your doors again.

Sometimes, it will bawl and scream in the voice of your head. The fragrance you vaguely remember. It will scrap your wounds and slowly make it bleed, leaving you uglier than ever. It will knock you down in the coffin and suffocate you in the dark loneliness which has always been there around you. It will walks in your dreams again. Acknowledgement it what it seeks.

Be fearless and acknowledge your pain. Look straight into its eye with assurance and say Thank You! “Thank you for being there, thank you for teaching me many things, for making me a little stronger than I was yesterday. Resounding the fact that pain and happiness are the two sides of the same coin. You will always be remembered as a dream because memories are where dreams go and die.”

Lay the skeleton down gently in the resting place and let the grouchy child know, your time is over. You are not going to look back or grieve over the past, for it is gone. It is time for you to be silent now. Let it die peacefully in your dreams. Let the stars align themselves in the dark sky and guide you home. Let the sorrows release themselves which are held captive. Let it bring your happiness back from the grave and make peace with your demons. But more than anything it will give you the meaning you are looking for in this chaotic world.

Soon the new day will break into dawn, the night shadow will flee and the sun will light up the sky and there you will wake up to the sound of the birds chirping  and soaring high in the sky, feeling a little lighter and wiser than yesterday.

Indeed it is a start of a new day.

Sudeepa Vyas

P.S For more random musings follow http://ramblesofanomad.wordpress.com/