Archive for the ‘Ruby on Rails’ Category

Recently I met a lot of startup founders and directors through Singapore Entrepreneur & Developer
community. While many of them are interested to provide both web and mobile solution for their
application, some of them are thinking to get the web version out first and think about mobile
applications later.

In either case, they need to think of an approach that’s more agile and easily extensible. Many
developers would follow the conventional way of building an application and adding components for
more platforms they would support. That not only makes them write the same functional features
multiple times but also make it less scalable as and when more users will start using the

I have jotted down an easier and more scalable approach which encourages in creation and adaptation
of an API based route. While in the first approach the number of controllers or methods are directly
proportional to the number of supported platforms, in the second approach they can leverage on the
single set of APIs. As and when they will support more platforms they can parse the API response
and display it as per the device form factor and designs. As most of the modern platforms like
mobile os, web technologies, internet TV, think-clients support XML or JSON parsing it will be
easier to re-use the same code or set of APIs to enable the solution for a new platform.

Probably one if the simplest mistake I have ever done. But got stucked
with this for an hour before I would figure out what was wrong.

Rails has got a very nice set of helper functions that would generate
the HTML for you. So to upload a file you would probably write something
like this in your view.

But with this if you will try to extract the file in your controller,

params[:model][:image].read will give an error:

NoMethodError (undefined method `read’ for “<file.jpg>”:String)

So how come the image file became a String? Well, if you have done a
silly mistake like me than you have forgotten to add the “multipart”
value in the form.

So simply add this to your form_for tag and it would work like a charm.

I spent a good amount of time in digging about another ruby gem.
RMagick is the magic gem I am talking about.In ruby, magic means
something amazing, something wow.You don’t have to scratch your
head to know about much of the background details. Just know how
to use it, and it’s all done!

RMagick is a binding from Ruby to the ImageMagick library. You can
use it to resize images, create thumbnails of images in a directory,
crop images, create watermarks for images and much more.

+ “rmagick” is the gems you need to include.

+ is a method returns an array if you will try to open gif
animation with multiple frames. In our case, we will get a single frame as it’s
a png image. And “.first” gives us the 1st frame which is the image itself.

+ change_gemometry method takes a new size as an argument and passes
the value to the associated blocks.

+ resize! method resizes the image and write saves the files with a new name.

You may like to try out other features of RMagick. Like conversation of image
formats. (PNG to JPG or others!)

I had to create a tag cloud for one of my project. I did a good search
in google and got an idea about the same. But for me the challenge
was to implement it in Rails m-v-c architecture.

And none of the current articles in the web helped me much. I wanted
to make it simple and efficient. So here’s the process I followed to do it.
Thanks to Maku and Pragmatic Rails book that helped me to get
off the ground.

A tag cloud is a collection of tags (you already have in the database or
the some contents of the web page). All you want is to represent them
in a visual depiction.

In my case, I already had a list of tags in the database. I just needed to
call them and display them.

In Rails in model-view-controller architecture.

  • Model represents data. So, a table in the database is nothing but a class in model.
  • View is responsible for creating either all or part of a page.
  • Controller supplies data to the view and receives events from pages generated by the view.

Nothing comes directly from model to view. Always data comes to the
view through controller and vice-versa.

# Find the list of tags in the action of a controller.
(let’s say this action name is tag)

# Count the number of times each tag is available in the database.

# Create a hash and store both the tag_names and tag_count.

# Pass the hash to view. (tag.rhtml)

# Display each tags and resize the tags as per as their frequency.

[The more a tag is repeated in the database the bigger it will be in the
page. Use some kind of stylesheet syntax to do the same. You can apply
additional styles for colors. You can write a short function in the
app/helper and call it in view to display the tags.]

I remember Mayank saying me couple of months back that ruby is a
very powerful programming language and Ruby on Rails (RoR) is
gonna be “THE future” of web development. He went ahead with his
mission of ApnaBill after that, a system which is entirely designed in
RoR. I started the journey little late – but it’s worth starting.

For last couple of nights, I have been playing with Hpricot which is
one of the most powerful ruby gems. It’s generally used for HTML
parsing. The beauty of hpricot is that it allows you to parse by XPath
and CSS tags.

And if you have firebug plug-in installed then the world of web is
yours. You can parse any tag by it’s XPath doesn’t matter if it’s badly
structured and there are 100 tables in a single page with out any div id
or class name.

Here’s few quick steps that you can start looking at:

1> Open the URL in firefox.
[I am not against other web browsers, but I just love FF]

2> Enable firebug for the page.
[Install firebug plug-in prior to that if you haven’t done so].

3> Right Click on the value that you want to parse and do inspect element.

4> It will open up the HTML headers.

5> Click on the thingy that interests you. And copy the XPath.

6> Note: Firebug adds additional “tbody” tags in the XPaths.
[Just ignore them and go ahead]

7> Parse the same in hpricot.