puneetpandey.com

Ruby on Rails Delivery Expert Blog (ALPHA)

Hey there! Thanks for dropping by Theme Preview! Take a look around
and grab the RSS feed to stay updated. See you around!

Category : Ruby on Rails

Jquery on Rails3 auto select

Hello Folks,

In earlier post of mine, we have seen, how to implement “Auto Select”, with the help of Prototype and Rails.

Now, that Rails-3 has been released, lot of changes are already made. In-built support for Prototype has been removed. Still if you wish to use prototype, all you need is to install its gem.

Here, I am covering the same example:

- Select ‘Country’ from the first drop down.
- Second drop down will populate the list of ‘States’. If you select any state:
- Third drop down will populate the list of ‘Countries’.

All I am using here is Rails-3, Rails-3 UJS, and Jquery.

I would also like to thank Ryan Bates, for the railscasts he provided to implement this using unobtrusive javascript.

Please download the application with the link provided and let me know your feedback.

System Requirements/Features Covered in this application.
- Ruby 1.8.7
- Rails 3.0.0 or higher
- Country -> State -> City Drop down
- Ajax Search

Steps to run the application:
- Create a schema. Change the database.yml accordingly.
- Run rake db:migrate
- Start the server.
- Please do read the README file.

jquery-autoselect

Regards

Don’t hesitate to try out new things in Ruby

Since last couple of months, I was trying installing Rails-3 on my Windows PC.
When I heard, that Rails-3 requires ruby-1.8.7 or higher, I instantly removed Ruby-1.8.6 and all its components and installed 1.8.7 from the fresh point.

I was happy, as my new system has Rails-3 now… Wow, what else I need?

But later, I found that my few of my old applications, which requires ruby-1.8.6 have stopped working. WTH!! Can’t it work with Ruby-1.8.7 now? With the investigation, I observed that, there were few libraries which were specific to 1.8.6 version and are not in 1.8.7 now. Some of them, I restored from 186 to 187 but were not compatible. Urrrgghhh!!! I am disappointed :-(

Now, I need some sort of version Manager, which will handle multiple version of Ruby. From here my search starts!!

After searching a lot on Google, and suggested by one of my friend, I found ‘pik‘.
Short Intro about Pik: Pik is a version manager for Ruby. You can handle/install multiple versions of Ruby and switch between them easily.

Basic Requirement: You must have atleast one version of Ruby, up and running on Windows.

Installation: ‘pik’ comes as a gem. To install pik, follow these steps:

Dependencies:
> my current plateform: ruby-1.8.6. From the command prompt, type:
gem install pik
> After the successful installation of pik, type:
pik_install C:\tools
> Add this to your Path Environmental variables
My Computer > Right Click > Properties > Advanced System Settings > Environmental Variables > Path

Once you’re done. Open a new command prompt and type:
pik list
It will add the current version of ruby into the list.
Now what’s so special about pik? How it can handle the multiple versions? To see that in action, in command prompt, type:
pik install ruby 1.8.7
and this will start downloading and once downloaded it will install 1.8.7, under:
C:\Users\PC-name\.pik\rubies\Ruby-187-p302

Awesome!! isn’t it?
pik list
It will show you the current version of ruby and the other ones.
pik use 187
Will use now ruby-187. Don’t believe me? :-x Then type:
ruby -v

I am super excited now :-D I want to install Ruby-1.9.2 now. Afraid? Still hesitate? Don’t be, just follow the instructions…
From the command window, type:
pik install ruby 1.9.2
Heights of happiness… It is installing 1.9.2 now!!
Path will remain the same, i.e:
C:\Users\PC-Name\.pik\rubies\Ruby-192p302

Do a pik list now and you’ll see:

Now, I am running multiple versions of Ruby on my Windows m/c and I can select any version by:
pik use 187
gem install rails --include-dependencies
rails s -p 4000

and can perform numerous operations… Yuppy!!!!

Here comes the another pain. Few days back I thought to work with Rhodes. The Mobile Framework for Ruby.

To install it, I went through the Rhodes website and followed the tutorials. I did:
pik use 187
gem install rhodes

I got make error :-x Why windows won’t work like the Linux does and why do we need to troubleshoot C-compiler problems?

Another day I spent to look around for a solution and finally came up with Devkit.

Short Intro about Devkit: Devkit is a windows executable program, which creates native extensions for your Ruby gems, which uses C-complier.

Installation: Download the Devkit (Development Kit) from Ruby installer website. After downloading, extract it to:
C:\devkit
After extraction, open your command, go inside C:\devkit prompt and type:
ruby dk.rb init
This will create the config.yml inside C:/devkit. Now you need to define all the Ruby versions inside that file, so that devkit will create native extensions for them. To do that, open that file and at the end of it, write these lines:

- C:\ruby
- C:\Users\PC-name\.pik\rubies\Ruby-187-p330
- C:\Users\PC-name\.pik\rubies\Ruby-192-p0

Once done save it and from the command prompt, type:
ruby dk.rb install
and you should see something like this:

Awesome, easy steps, isn’t it? Now, what else you’re looking for? Go ahead, install any gem for any version of ruby without any hesitations :-)

Please let me know if you’re stuck or need any help. Good comments will always motivate me to write more and more and help the Ruby Community keep growing.

References taken from:

Freeze gems in Rails 3 Application

I have seen lots of forum, where developers keep on asking: “How would I freeze the gems to my Rails-3 application?”

Take this URL for example: http://bit.ly/gFzN6Z

One developer says that: localize your gems by running this command: bundle install –vendor/gems

Others refuses to go with this way. Here’s why?

  • We are moving away from the concepts of bundler
  • This will install the gem dependencies as well in your vendor/gems directory, which we don’t need at all. We need only those gems which are specific to our application.
  • You don’t need to unpack the gems and check them in to your app, because it doesn’t matter: you’re guaranteeing the same versions are being called regardless of where they are installed, which will likely vary from machine to machine anyways (.bundle/ should not be checked in to the repo) – so why stick another 60-80 MB of files into your repo that you won’t ever be changing or using?

So, there is no way you can localize your gems in a Rails-3 application. There are other ways you can perform in order to better use of bundler gem.

  • Use specific versions in your Gemfile and run bundle install –deployment on each target system where you need the exact gem versions.
  • While defining a new gem in Gemfile, assign a path to it. For example:
    gem 'paperclip', '2.3.15', :path => './vendor/gems/paperclip-2.3.15'
    Bundler will assume that it has to get the gem from vendor/gems folder. Once you’re done. run bundle install

Custom Validation in Paperclip

In this short tutorial, I will explain you how you can add custom validation messages on the Attachment if you’re using Paperclip plugin or gem.

Way 1. This will work if you’re running your rails application on Windows Machine.
Solution: a) install mimetype-fu plugin.
b) Once installed, open Model, where paperclip settings has been defined. In my case, its User Model. Open it and add:

has_attached_file :avatar
validate :prohibited_ext
protected
def prohibited_ext
errors.add(:avatar, “Format #{File.mime_type?(avatar.path)} is invalid!”) unless File.mime_type?(avatar.path) == “application/exe”
end

Upgrade Rails Performance

In this article, we will try to Enhance our rails application performance.

To know, how is your rails application is performing on different environments, you must need some plugin or gem. There are many gems/plugins already available which you can use and analyze the performance.

First of all, I would like to list down those, which I will cover in my next tutorials.

Once you configure anyone of those, you’ll come to know how is your rails application is performing. I personally advise to use New Relic or Rack-Bug.

There are several ways, where you can upgrade you rails application performance. Let’s see what are those and how they work…

  • Enable Caching
  • Load Less SQL Queries
  • Avoid using before_filter for loading heavy data

Send Tweets from Rails Application

So, we’re continuing from here: http://puneetitengineer.wordpress.com/2009/08/12/send-tweets-fr…ls-application/

So far we’ve covered the basics of it, now its time to create some controllers…. Open your command/terminal and fire this command:

ruby script/generate controller twitt

Once you’re done, open this file and write the below code inside it:

class TwittController < ApplicationController

gem(‘twitter4r’, ‘>=0.2.0′)
require ‘twitter’
require ‘twitter_search’
require ‘rubygems’
before_filter :authorize, :except => [:login, :authenticate, :save_users, :twitter_cred]

def login
@user = User.new
end

def authenticate

@user = User.new(params[:user])
client = Twitter::Client.new
if client.authenticate?(@user.username, @user.password)
@username = User.save_users(params[:user])
session[:user_id] = @user.id
session[:username] = @user.username
session[:password] = @user.password
redirect_to :action =>’post_status’
else
render :action =>’login’
end

end

end

Now, open /app/views/twitt/login.html.erb and paste below lines:

<% form_for :user, :url => { :action => :authenticate } do |form| %>

<%= label :user, :username, "UserName:" %>
<%= form.text_field :username, :size => 30 %>

<%= label :user, :password, "Password:" %>
<%= form.password_field :password, :size => 30 %>

<%= submit_tag "login" , :class => “submit” %>
<% end %>

Integrate Oracle with Ruby

Since most of the rails application is now using various databases, this article will show you how you can use ORACLE database with rails.

For that, we need Oracle XE

Hi Guys,

In this article I will cover how to use cucumber and Factory Girl to write Stories in Rails.
TDD(Test Driven Development) is the most powerful and most approachable concept in most of the programming language. When it comes to rails, it provides a convenient and simplest way to write Unit test cases and the stories for your controller, without writing complex lines of code.
Let’s see how you can cover the test coverage for your rails application.
IMPORTANT: The concept behind using cucumber, so that we can write the ‘stories’, which will tell us how our controller’s method behaves when its called.
To get started, install the cucumber gem on your machine by typing:
gem install cucumber
and you’ll find progress like this:

Next, you need to install rspec and rspec-rails. To do that type:
gem install rspec
gem install rspec-rails

What is Mongo-DB

What is MongoDB?
I saw many forums, visited many blogs, people/programmers are talking about MongoDB a lot. I was wondering what this is! Why developers are showing more and more interest in this. I started searching around and now sharing with all of you, who are not friendly with MongoDB or who’re familiar but don’t know how to work on it.

First of all let’s define mongoDB in terms of technical aspect
a) MongoDB has collections of data, not tables.
b) Its a document-oriented database.
c) MongoDB has namespaces for data.
d) You don’t ever need to worry about migrations due to Mongo’s schema-less nature.
e) You can define indexing options too.

So far, what my learning works here that it has nothing to do with the migrations, tables. In short if I say, it is a database in a documented form, a separate entity as compared to MySQL or different databases, am I wrong?

Ok, try to put your brain to understand this, because from here I will try to cover some real world examples to explain how it works… :)

How to install MongoDB
A) gem install mongo
It will install mongo with bson.. something like this screenshot

B) gem install mongo_mapper

I think, these two gems are sufficient to give you some kind of acceleration. Let’s proceed further and create a model, fire this command
C) ruby script/generate model Book –skip-migration

D) Let’s enter some fields into it, as I said it has nothing to do with the migration, so what are the columns that we want, we will create here itself, See how:

Rails 3, Its here!!

In these days lots of people are talking about RAILS 3, the biggest and most happening version as compared with the previous ones. There are many changes in each and every part of the application.

Here, we will cover the changes, dependencies, how we install rails 3 on windows and linux and a basic sample RAILS 3 application..

So Here we go:
1st We will install rails 3 on windows.
Note: To Install rails 3, you must be having ruby-1.8.7 or higher, So If you’re running with ruby-1.8.6, it’s time to upgrade now.
To do that download the executable version of ruby(either 1.8.7 or 1.9.1) click here and download Ruby 1.9.1-p378 (RC2). Once done run the setup and check both the option as per the below image.

Once done with the installation, You need to copy 3 DLL files under /Ruby19/bin folder and these files are:
1. readline.dll (Download from here)
2. ssleay32.dll (Download from here)
3. zlib.dll (Download from here)
4. OpenSSL(Download from Here)
For better examples, you can also refer UKNatureBlog

Once you copied all the dependencies, now it’s time to start few gems, which will help us to install rails 3.0.
Do update your system with this:
gem update –system
Because bundler and i18n looks for rubygem >= 1.3.6
To install these gems, open up command prompt and type:
gem install tzinfo builder memcache-client rack rack-test rack-mount erubis mail text-format thor bundler i18n

and you’ll find progress something like this:

You have to install rack as well in order to install rails 3. To do that type:
gem install rack
Once done with it, now its time to install rails 3.0, so do this:
gem install rails –pre

Note: Don’t worry if your installation fails somewhere in the middle. Important is your gems(like activerecord, activeresource etc.) should be installed successfully, no matter ri and rdoc encounters an error.

So far we’ve installed ruby-1.9 and rails-3.0. Now it’s time to create your first rails application. Here I am assuming that you already have mysql installed in your system. So open up your command prompt and fire this command.
rails myrails3_app -J -d mysql
and you’ll see progress like this:

Other possible parameters:
1. –skip-activerecord

Now let’s look at the directory structure which rails-3.0 has created for us:

At first glance you will notice that, there is no change in the directory structure, everything is the same (except for two files config.ru and Gemfile).

We will create a sample RAILS 3 Application in our next tutorial soon.