Blog

Filtering all blogs tagged as "rails."    Clear filter

Rails Helper including another helper problem in Development mode

I had a tough couple of hours fighting with Rails helpers , my desire was to include a helper inside another one to have a method of that other helper aliased, so I wrote the following:
 
module MyHelper

  include TheirHelper

  def their_method_with_my_mehtod
    puts their_method_without_my_method
  end

  alias_method_chain :my_method, :their_method

end
That worked with the first request, but subsequent requests, throwed a "Stack overflow error", because in development mode, helper is re-included and the two methods "their_method_with_my_mehtod" and "their_method_without_my_method" became pointing to the same thing, so the solution was:
 
 
module MyHelper

  include TheirHelper

  def their_method_with_my_mehtod
    puts their_method_without_my_method
  end

  alias_method_chain :my_method, :their_method unless method_defined?(their_method_without_my_method)

end

Another way to do that is through writing "unloadable" at the module start
module MyHelper

  unloadable # prevent it from being unloaded in development mode
             # other wise, a stack overflow exception will be 
             # thrown due to the alias method chain being called twice
  include TheirHelper

  def their_method_with_my_mehtod
    puts their_method_without_my_method
  end

  alias_method_chain :my_method, :their_method unless method_defined?(their_method_without_my_method)

end

Read More

Rasem, draw SVG images using ruby1

The web is moving to HTML 5 where SVG images are the standard images to use. But how to generate SVG images to include? You might draw static SVG images and include them in your web site but this will not help with generated SVG images with dynamic data. Of course,  you can read the complete specification of SVG and generate your own images; these are a few hundreds of pages; good luck with that :)

The elegant solution is to use Rasem, a pure ruby gem, to generate SVG images. Rasem allows you to describe your SVG images in pure ruby code. You can either save the generated image to a file or output it to a string to use it with your HTML 5 page. Rasem generates standard SVG images that are compliant with the W3C standard.

gem install rasem

and have fun!

Checkout the project main page at: http://github.com/aseldawy/rasem

You can find examples and usage instructions there.

Read More

Heroku currently active username, How to change on Mac OSX or Linux ?!

If you are already using Heroku on your Mac or Linux with some email like me@x.com and you want change the currently active username on your Mac to other like me@y.com, you should do the following:

Read More

PostgreSQL 9 bytea type problems3

While supporting a long-term ROR web application, I got into a weird problem while setting up the environment to start up the app on my platform (Ruby 1.8.7, Rails 2.1.1, PostgreSQL 9 and Ubuntu 10.10). Symptoms of the problem show up when trying the following scenario:

In a development environment, start up the server, open a browser and enter the usual ' localhost:3000 ' url to start the app showing home page; till now, no problems, but, when making any action (clicking a link or even trying to refresh the home page) causes the browser to generate a '500 Internal Server Error'. A problem in the session you might think, so did we.

Following the log files showed that the failure occurs when trying to unmarshal some binary data retrieved from the database; the data been saved in the database are different when retrieved!  More digging down took us to the column in database where the binary data were saved. The column type was bytea, which is basically a variable length binary string. The problem turned out to be that, for this specific data type (bytea), the output representation of PostgreSQL 9  is different from previous versions. PostgreSQL 9 introduced a new 'hex' format for output, this format is different from the old 'escape' format that older versions of PostgreSQL used to provide. That was the problem, we save data in the database, expect it to be in 'escape' format when retrieved, but we get it in 'hex' format.

The solution is rather simple, tell PostgreSQL to return data in the old, familiar 'escape' format. This can be achieved using the following code

 

ALTER DATABASE database_name SET bytea_output TO 'escape';

 

 

This, being executed in PostgreSQL command line, guarantees that you get the data in the format you expect.

Read More

Access oDesk APIs in Ruby and Rails2

BadrIT has developed libraries to access oDesk APIs. Checkout Ruby Desk gem for desktop applications and its variant RoR Desk plugin for Rails applications.

Update 13-7-2010: Ruby Desk and RoR Desk are now officially listed on oDesk developer section. See ttp://developers.odesk.com/API-Libraries and http://developers.odesk.com/Examples.

Read More
« Previous Entries