Saturday, November 6, 2010

Cucumber test for select box!

The behavior driven development is really fun. First of all you determine the flow of the program and then only write real codes. This technique is really effective as it saves time while programming, since the flow is already determined, the chance of hindrance is very low.

A simple test for seeing something in select box is:
Given I am on the index page
And the "Nepal" should be selected for "Country"
(where "Nepal" is your selected value and "Country" is the id/label of the select box)

The step definition for this step is
Then /^"([^\"]*)" should be selected for "([^\"]*)"$/ do |selected_value, field|
find_by_id(field).value.to_s.should == selected_value
end

Using ajax call with file field!

Sometime ago I had real trouble using file field and ajax call together. I wanted to browse a file and display the contents in the same page. Then after some research I came across gem called "remotipart". This gem is really helpful.

To use this gem follow these simple steps
1. Include "gem remotipart" in your Gemfile
2. Bundle install
3. type this in your console: rails generate remotipart
4. Include jquery-1.4.2-min.js, rails.js, jquery.form.js and jquery.remotipart.js as your javascript files
5. Suppose you are using this in you new.html.erb file, then do this
<%= form_for @product, :html => { :multipart => true }, :remote => true do |f| %>
<%= f.label :file_upload %>
<%= f.file_field :file_upload %>
<%= f.submit %>
<% end %>
6. In your create controller include: format.js {} instead of format.html{}
7. In create.js.erb file :
<%= remotipart_response do %>
//Write your javascript code here.
<% end %>

If you have any confusions go to https://github.com/formasfunction/remotipart. This site really helped me a lot.

Sunday, September 19, 2010

Using cygwin for rails!

My previous posts helped you only to install cygwin. But this time, I tried using cygwin for all rails commands.

The first step is to install rvm through your cygwin bash shell. RVM is a ruby version manager that allows you to have multiple versions of ruby installed. So, when you need to use ruby-1.8.7 or ruby-1.9.2 for certain projects you can shift from one to another.

I tried using curl command to install rvm but I was unsuccessful. So, I installed rvm as a gem. By typing the command

~ gem install rvm

And copied:

function gemdir {
if [[ -z "$1" ]] ; then
echo "gemdir expects a parameter, which should be a valid RVM Ruby selector"
else
rvm "$1"
cd $(rvm gemdir)
pwd
fi
}
in my .bash_rc file also you need to copy:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.

into your .bash_profile.

If you chose ruby initially when installing cygwin, the version of the ruby will be 1.8.7. However, the most current version of ruby is 1.9.2-p0. So, if you don't want to be lagging behind. In your cygwin shell, type
~ rvm install ruby-1.9.2-p0

You can see whether the rvm installed latest ruby by
~ rvm list

(for more commands of rvm try ~rvm usage)

Though you have installed latest ruby, you need to tell rvm to use that version of ruby. So,
~ rvm default 1.9.2-p0

Also you would want to use global gemset because global allows you to share gems for that version of ruby. For example, if you have two gemsets, say, projectone and projecttwo. However, they may require same gems, devise (for user login). In this case, if your global gemset has devise, then both the gemsets "projectone" and "projecttwo" can have access to that gem. So, for using global gemset
~ rvm gemset global

Similarly you can create gemsets by
~ rvm gemset create projectone

Now you would like to install gems for latest ruby. So download "rubygem 1.3.7" from in "path\to\Cygwin\home\Home\.rvm\gemsets\ruby". Also install latest rails by
~ gem install rails --pre

(In my case, the last command installed rails-3.0.0.rc2 which is not latest. So, after the rc2 was installed I tried "~ gem update rails" and it worked!)

I am using cygwin for rails commands because sometimes ruby interpreter does not allow you to install several gems however, this caveat is overcomed by cygwin. I hope this post helped you in installation.

For more information on using rvm visit beginrescueend.com, it will surely solve all your confusions if you have any regarding rvm. Have fun with rvm! ;)

Sunday, September 12, 2010

Going deep in rails!

The previous blogs were only theoretical. To learn ruby on rails in details please visit http://edgeguides.rubyonrails.org/. Follow the guide and you will have clear concept on programming in ruby on rails.

Sunday, August 29, 2010

Creating your first Ruby program!

Create a simple file and type

puts "Rails World"

save the file in .rb extension (example: rails.rb).

On the command line, go to the folder containing your "rails.rb" and run

>> ruby rails.rb

Congratulations, you created your first rails application.

Installing Ruby on Windows

After installing cygwin, next step is to install Ruby on your computer. For installing ruby refer to rubyonrails.org. Follow the simple steps specified there. For installing latest Rails:
gem install rails --pre

Now, it will be easy to install bundler also, because rather than typing gem install.... it would be easy to specify your gem requirements in Gemfile and running
bundle install

So, for that run the pre-requisite is
gem install bundler --pre

Furthermore, I am using Sqlite3 database server, you will have to install sqlite3 from http://www.sqlite.org/download.html.Choose sqlitedll-3_7_2.zip (265.70 KiB)and copy paste the exe file in your ruby/bin and do
gem install sqlite3
gem install sqlite3-ruby #(for connecting sqlite3 and ruby) 


If you just want to change the database server, you can simply change the adapter of your application_folder/config/database.yml.

Note: I am using "Ruby interpreter" for rails commands and cygwin for git commands.

Saturday, August 28, 2010

Brief Overview of Ruby on Rails

I still remember when we had to do our final year project, we had to write tons of code for building small application in programming languages other than on rails. However, Rails has saved the life of many programmers! In rails, we do not have to start from the scratch like making folders and files one by one. This is done by the rails itself. Write a simple rails command and all the required folders are generated. Isn't it awesome. Rails is the platform and Ruby is the programming language. The life of Ruby programmer is made easy through RubyGems, which contains files to install. They are like plugins, for example, if you want user authentication you can simply install gem called devise(I will come to this later on).

Moreover, Rails is based on MVC(Model, View and Controller) architecture. Models deals with data and database. View is what your user-interface will look like. Finally, controller connects the model and view. It's the job of the controller to control both the model and the view.

You will find many useful materials on internet but I could not stop myself from this brief introduction to "Ruby on the Rails". Finally, welcome to the "Rails World" (apart from "Hello World" ;) ).

Installing Cygwin in Windows

Installing rails directly on windows can be really troublesome(trust me on this). So, for windows lover (me included) I think installing cygwin (linux emulator) before installing ruby is a good idea. You can install cygwin from http://www.cygwin.com/. In the installation window, search for the following requirements: make,curl, ruby and git (for version control if you are using git) in your installation.  Also, select g++ and gcc if they are not already selected.