Archive for January, 2009

Locally generating rails documentation in mac & linux.

January 30, 2009 , Written By  sdhar.

It is very easy to build a local copy of rails documentation and guides. Open terminal and type these commands.

  1. $ cd /tmp
  2. $ rails doc
  3. $ cd doc
  4. $ rake rails:freeze:gems
  5. $ rake doc:rails
  6. $ rake doc:guides
  7. $ sudo mv doc /opt/rails-doc

After completing these steps open browser and type

  1. file:///opt/rails-doc/api/index.html //api.
  2. file:///opt/rails-doc/guides/index.html // guide related to rails development.

organizing mailers and mailer templates

January 24, 2009 , Written By  Surendra Singhi.

When working on a large Rails project, it can be a kludge having the mailers present among the views and other model files. A nice technique to clean up things is to put all the mailers and associated views in the same folder. If you don’t know about this technique then go read it first, and then come back here.

The benefit of having the ApplicationMailer is that you can define the host for the mailers, include ActionController::UrlWriter, other setup methods in a single file and have all the mailers inherit them.

The patch written for action mailer to not use layouts for text/plain email was causing problem with exception notification plugin, so I have modified it slightly so that it checks whether the file parameter passed to it responds to content_type or not.

  1. def candidate_for_layout?(options)
  2.   (!options[:file] || !options[:file].respond_to?(:content_type) ||
  3.      options[:file].content_type != ‘text/plain’) &&
  4.      !@template.send(:_exempt_from_layout?, default_template_name)
  5. end

Further to clean things up this patch can be moved to ApplicationMailer to ensure that it is only applied for all our mailers and not for other mailers which may be used elsewhere.

simple search engine optimization steps

January 23, 2009 , Written By  Surendra Singhi.

Here are few simple and easy steps which you can take to ensure your website is search engine friendly.

  1. Make sure that your page urls are descriptive. For example look at the post urls of this page. They are of the form http://blog.kreeti.com/seo/simple-search-engine-optimization-steps. This is very easy to do in rails. Add the following code to the models:
    1. def to_param
    2.     "#{id}-#{name.gsub(/[^a-z0-9]+/i, ‘-’)}"
    3. end

    and then while linking to posts, use code like

    1. user_path(@user)

    and not

    1. user_path(@user.id) # do not use this
  2. Ensure that your pages have a meaningful page title, meta description and meta keywords. Title and description should be preferably unique for each page, and relevant to the page. One simple strategy which you can follow to do this in rails is, in the layout file add the following code:
    1. <title><%= @page_title || "Default page title" %></title>

    and similarly for meta description and keywords.

    Then in your actions you can override the default values

    1. @page_title = "New title"
    2. @page_description = "new description"
    3. @meta_keywords = "keyword1,keyword2,.."
  3. Make sure that all links to meaningless and duplicate pages have rel=”nofollow”. For example, pagination links, links which just sort the page content in a different order, etc., all should have rel=”nofollow” for them. This will ensure that the search engines, when propagating the authority of your page to other linked pages, doesn’t waste them on useless pages, or duplicate links.
  4. When multiple items on a page are linking to the same page, make sure that all the links with irrelevant terms have rel=”nofollow”, only the links with terms which aptly describe the page shouldn’t have rel=”nofollow”.
  5. Duplicate versions of your page, say with a different sort order, example a link like /users/recipes?order=created_at, should have
    1. <meta name="robots" content="noindex, noarchive" />

    in their head element.

  6. Your stylesheets, javascript files, images used for styling purposes, non-important pages like sign in, join, privacy policy, terms, should all not be indexed. To ensure this, create a robots.txt file like the following
    # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
    User-Agent: *
    Disallow: /account
    Disallow: /session
    Disallow: /images/
    Disallow: /stylesheets/
    Disallow: /javascripts/
    Disallow: /flash/
    Disallow: /opensearch.xml
    Disallow: /404.html
    Disallow: /500.html
    
    Allow: /

Sign up for Google Webmaster and Yahoo Site Explorer, and add your sites to them, and authenticate that your are the owner of it. Both tools provide very valuable information in optimizing a site for search engines.

Rails 2.2.2 - exception notification and spawn plugin

January 1, 2009 , Written By  Surendra Singhi.

In Rails 2.2.2 the send! method from ActiveSupport has been removed,this broke the exception notification plugin. So, get the latest release from the github.

Also, the introduction of database connection pools breaks the spawn plugin. So, switch to the latest release of that as well.