Tips and Tricks in Ruby on Rails

  • Use escape_javascript: When you are making an ajax call and in response sending JS that will insert the content in an HTML element, make sure you run escape_javascript on the content. For e.g.  $(“#div”).html(‘<%= variable %>’). In this case if variable’s value contains a quote ‘, the resulting JS that would be rendered in browser: $(“#div”).html(‘value_’_with_quote’). This will result in error on client’s browser and since server is not aware of the error, you do not get any notification. That makes this error worse.  If you use $(“#div”).html(‘<%= escape_javascript (variable) %>’); the resulting safe js would be: $(“#div”).html(‘value_\’_with_quote’)
  • addition of a nil string: Many a times I need to do an addition involving a ruby variable. For e.g. + ” extra string”. In case is not set, this statement gives an exception and errors out on the account that + operator does not accept a nil operand. You can either run a check on each variable if it is a nil, or you can simple add .to_s to save from the error. It becomes: + ” extra string”. ps: nil.to_s => “”
  • Keep all css files directly under /public/stylesheets: Most of us use :cache => true when specifying css files in our layout. This helps us in many ways by consolidating all css files in  /public/stylesheets/all.css and thus saving from multiple requests from browser when loading css files. However this has a problem, whey you deposit your css under a sub folder. Eg. you have a plugin css file /public/stylesheets/category/some.css and it refers to images which are also kept in /public/stylesheets/category/images. Now when some.css gets included in /public/stylesheets/all.css, it is not able to refer to images in /public/stylesheets/category/images. It will try to find those images in /public/stylesheets/images folder. You may change some.css to refer images from a different folder but then if you upgrade plugin, you do this every time! It is better to discard the folder “category” and keep some.css under /public/stylesheets. Have a common folder of all the ‘images’ of all the css files.
  • Caching – Pitfalls: Once you enable caching in your code, there are various pitfalls that you need to take care off:
    • content_for no longer works if called from a cached block. If you are setting your page title in layout and running a content_for in each view file to populate the title, you need to step back. If you action cache your page, content_for being a ruby code will run only once after setting up cache it won’t run and hence no title set hereon.
    • caches_action => :index, :layout => false. With this your content_for code will not run even for first time. Seems rails does some fancy stuff to enable layout => false
    • No more protect_from_forgery. Your rails form contains the authenticity_token cached during the first call. So for subsequent calls, stale authenticity_token is served which breaks the code
    • Railcast 169: Describes a nice way to run dynamic pages with page caches enabled.