Jul 12, 2011

Rails 3, RoutingError crashes the server

I have encountered this weird behavior. It took me 6 consecutive hours to solve it by debugging the whole Rails code.

In this post, someone suggests that we switch from WEBrick to Mongrel.

I have found out that the code piece which causes the problem is (It is located in C:\Ruby192\lib\ruby\gems\1.9.1\gems\railties-3.0.9\lib\rails\rack\log_tailer.rb):


module Rails
module Rack
class LogTailer
.
.
.

def tail!
@file.seek @cursor

if !@file.eof?
contents = @file.read
@cursor = @file.tell
$stdout.print contents
end
end
end
end
end


If you blank this method, WEBrick works fine again. I have done an intensive test on it with a lot of RoutingError thrown.

You can use this patch. Put it in the environment file:



module Rails
module Rack
class LogTailer

def tail!

end
end
end
end


The downside of this is that you won't see debug messages on your console.

To bypass this problem, you can use log4r to output debug messages on console instead.

Work like a charm for me.

Jul 11, 2011

Install MySql2 properly on Windows for Rails 3

You will need to configure your Bundle to use some flags by:


bundle config build.mysql2 --with-mysql-lib="C:\xampp\mysql\lib\opt" --with-mysql-include="C:\xampp\mysql\include"


If you cannot find lib\opt and include folder, please install MySql Connector/C. The folders will be there.

Then you must use mysql2 version 0.2.6 for Windows. Other versions do not work.

In your Gemfile, you should have this:


gem 'mysql2', '0.2.6'


And then you can run:


bundle install (or update)


This will solve all your problems, including "incompatible character encodings: UTF-8 and ASCII-8BIT".

Jul 9, 2011

Ruby global variable cheat sheet

$name program-defined global variable
$! latest error message
$@ location of error
$_ string last read by gets
$. line number last read by interpreter
$& string last matched by regexp
$~ the last regexp match, as an array of subexpressions
$n the nth subexpression in the last match (same as $~[n])
$= case-insensitivity flag
$/ input record separator
$\ output record separator
$0 the name of the ruby script file
$* the command line arguments
$$ interpreter's process ID
$? exit status of last executed child process

Rails plugin init.rb technique

Never ever do this in your init.rb


require 'db_migration'


Sure, Rails will automatically look into your plugin lib folder. But if another file with identical name is loaded before (from other plugin), then Rails won't look anywhere. It'll just grab that file.

Of course this is a behaviour that you don't want. You'd like each plugin to have its own space. Then, do this:


File.dirname(__FILE__)+'/lib/db_migration'

Jul 8, 2011

Rails Patch to obtain a current template

There are 3 types of rendering: Normal, Partial, and Layout.

We have to hook all 3 types.

The template is stored in @last_template.



module ActionView::Rendering
alias_method :_render_template_original, :_render_template
def _render_template(template, layout = nil, options = {})
@last_template = template
_render_template_original(template, layout, options)
end
end





module ActionView::Layouts
alias_method :_render_layout_original, :_render_layout
# Contains the logic that actually renders the layout.
def _render_layout(layout, locals, &block) #:nodoc:
@last_template = layout

_render_layout_original(layout, locals, &block)
end
end





module ActionView::Partials
alias_method :_render_partial_original, :_render_partial

def _render_partial(options, &block) #:nodoc:

prefix = self.controller_path unless options[:partial].include?(?/)
@last_template = self.find_template(options[:partial], prefix, true)

_render_partial_original(options, &block)

end

end

Stupid Rails3 with Missing template and render_to_string on partial view


render :json=>{:ok=>true ,:new_row=>render_to_string(:partial=>"row",:locals=>{:entity=>entity,:field_set=>SHOW_FIELDS,:is_new=>false}), :entity=> entity}


This gives you a "Missing template" error, even the view exists. I have found that this is because an AJAX request on Rails does not include the view format :html.

In order to fix it, just append .html.


render :json=>{:ok=>true ,:new_row=>render_to_string(:partial=>"row.html",:locals=>{:entity=>entity,:field_set=>SHOW_FIELDS,:is_new=>false}), :entity=> entity}


And it will work.

Stupid Rails 3.0.3

mysql2 version 0.2.6 is the only version compatible with Windows :S