Monday, October 22, 2012

Getting started with Ruby On Rails

I have been an ASP.Net developer for 6 years and in the last year or so I have been working only on ASP.Net MVC, I truly think its great! And much better then ASP.Net.

but... I had the need to learn something new and open source, have a little diversity and stop being just a "Microsoft developer" which is pretty common in Israel... anyway I choose Ruby On Rails 3 which looked very cool, and of course its MVC too so I knew it will be easier for me.

I will describe the step by step process I have taken in order to move to rails.

Part One - OS!


Lets choose an operating system, after a bit of reading (thank god for stackoverflow.com) it was clear that the natural environment to develop Rails is a mac, but i don't have mac and i am not a big fan of Apple in anyway, but I do have experience with Linux, so I decided to go with Linux.
It really doesn't matter, plenty of rails developers are using Linux...
So the first step will be to download Ubuntu 12 installer.
It's not like the old days where you had to re partition everything and format and have a crappy GUI for the install process only to find out in the end that it didn't work.
Today Ubuntu offers a cool installer which will install Ubuntu side by side
with your windows. download ubuntu installer this is pretty easy and i don't have much to say about it. Just install it! Then restart your computer and you will be able to choose if you want to go into your Linux or windows. (very nice...)
Choose Linux of course! If its your first time on Linux don't worry you are going to love it!

Part Two - Installation!

Now that Linux is up and running its time to install
Ruby and Rails (rails is a gem for ruby)

First things first, open the terminal (you can just press the win-key and type terminal) never close the terminal, you will use it all the time.






Ubuntu has a tool called APT and you can use it to install and manage applications.
Lets first update it. To do so, run this command: sudo apt-get update
(sudo is the command for running commands as the root user so it will ask for your root pass).
After the update we can begin with the install. If you will google installing rails on ubuntu you will get this page in the ubuntu site its really out dated and talks about ubuntu 9 and installing rails and gems with APT. This is highly not recommended, so i found this post: 
better simple post that explains how to install ruby, rails, gems on ubuntu

Tthese are the steps you must take:
  1. Install curl
  2. Install RVM
  3. Install all the libraries mentioned
  4. Install ruby
  5. Install rails
  6. and stop!

Now its time to talk about databases.  Usually rails development can be done with sqlite or MySQL but because I choose to work with heroku (later on i will explain why) I choose to work with PostgreSQL.  You can install MySQL too but be sure to install PostgreSQL


install guide for PostgreSQL 
Run this command as well :
sudo apt-get install libpq-dev
Be sure to install pgadmin3 so you can manage your database later on with a nice GUI. so like before: sudo apt-get install pgadmin3
to open pgadmin3 just go to your dash home and find it there or type pgadmin3 in your terminal.


Any problems?
When I tried to install the rails gem i got this error: "It seems your ruby installation is missing psych (for YAML output)" I fixed it by following this  question on stackoverflov.
The fix is 3 easy commands for you to run in your terminal: 
cd ~/.rvm/archiveswget http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gzrvm reinstall 1.9.3

Now you can try an run gem install rails again and this time it should be fine (do it from your home directory, type cd then just enter)

Part Three - IDE!


We now have new OS, Ruby On Rails and PostgreSQL, time to install an IDE so we can start coding!
As ASP.Net developers we are used to the amazing Visual Studio, but its time to learn something new.. So lets choose an IDE
In the mac rails developers use TextMate and on ubuntu some use Gedit with a customization called Gmate, tried it for a few days... didn't like it. I am used to a full a feature IDE so the solution is called Netbeans! This is discussed on this thread so Netbeans is really nice, lets go ahead and install it, run: sudo apt-get install netbeans . Netbeans doesn't come with built in Ruby support (anymore) we must install it as a plugin. 
  • Open netbeans (to open Netbeans just go to your dash home and find it there or type Netbeans in your terminal) and go to: tools -- > plugins
  • In the plugins window go to settings tab and on the left check Plugin Portal
  • Then go to the Available Plugins tab and press Reload Catalog then you can do a search on the right for "ruby".
  • Check the checkbox for the ruby and rails plugin and click install. 
  • Netbeans will restart and you will have Nebeans with Ruby.

To try it out you can just create a test app, and open it with Netbeans.
  • You can try it out now. in your terminal run: rails new testapp.
  • Then run cd testapp followed by: bundle install .
  • Your test application is ready to go. 
  • Open Netbeans and click on new project.
    In the categories on the left click Ruby and on the right choose 
  • Ruby on Rails application with existing sources 
  • give your project a name and browse to find your testapp directory in your home folder. 
Your first rails project is now loaded in your IDE. You can start coding in Rails!


Part Four - Learn Rails



Everything is setup, now its time to start learning and coding Ruby On Rails.
So grab this excellent book: Rails 3 in action
and start seeing all these great video casts at: railscasts.com 
Another great resource is: Rails guides.
I started with these three but also thought of an app to build and just started building it. there is no faster way to learn then to just get your hands dirty in the code and start running into problems, so you can solve them.



Part Five - Deploy


By now you surely want to just get started and upload your first site. but how?.. and where? One of the popular places to host your rails apps is on heroku.com which is a cool cloud application platform, with addons you can install at a click of a button. You can sign up for an account which gives you 5 free repositories for your apps. If you are just experimenting you can keep it as a free account, but if you plan on building a full app with lots of traffic... Well.. that's when you start paying.
So heroku is very easy to work with and very well documented with lots of helpful tutorials. Just read through there dev center devcenter.herouku.com and you will be fine.

Uploading your code is as easy as writing: git push heroku master
I like it. plus even with a free account you can add your own domain names.
Your heroku account will have PostgreSQL set-up right out of the box. and that's why we installed it. You can still keep your development environment on sqlite but I prefer my development environment the same as my production one so I use Postgre in my local machine as well. (this is also the recommendation from heroku) you will notice how easy it is to change database to Postgre in Rails. In this heroku tutorial you will see just how easy it is.

To Summarize

I am having a lot of fun with rails. I already built two  small applications in just a few days. you can clearly see the advantages of it.
It's always good to learn new stuff especially in the software development field.
And why be limited to only one technology? Hope this guide helped you out in anyway.

May the force be with you



Friday, October 5, 2012

Cancel button for KnockOut form


I recently built a dynamic form using KnockOut.
This is how that page worked:

A user selectes a person from a list, an AJAX request will post the person id to the server which will then return  a JSON object of that person info (phone, name, age...) I didn't know which info will come back for that person. this is why the form is dynamic. anyway I decided to use a data-bind of template (there were a few a kinds of fields not just text boxes) so template with jQuery templates created a very cool dynamic form.

Now a new demand came saying I should add a cancel button to the form. after a quick Google search I came across this post: guarding your model in a very very good blog for KnockOut knockmeout.net,
anyway accepting changed for a specific observable is nice.. but didn't fit my needs for a full form cancel button. so I decided to go with a simple solution. I will keep my original native JSON object that came back from my AJAX request!

so it will go something like this:


// callback function from server
function AjaxCallback (model) {
   // page is a namespace 
   var tempModel = {};
   // important making a new object (tempModel) so 
   // the copyModel won't be a reference
   $.extend(tempModel, model);
   page.copyModel = tempModel; 
   UpdateModel (model);
function SwitchModel (modelJSON) {
   ko.mapping.fromJS(page.copyOfModel, {}, page.model);
   // we will need to apply the bindings again so the view will update.
   ko.applyBindings(page.model);
}
function CancelChange () {
   UpdatehModel (page.copyModel);
}

function SavePerson () {
   var jsonModel = ko.mapping.toJS(page.model);

   // ------- save the person through ajax

   // don't forget to update the copy model so if 
   // the cancel button will be
   // clicked the last changes won't disappear.. so:
   // better if placed in the success callback of the ajax request
   page.copyModel = jsonModel;
}


page.model is a model i assume you already have on your page. I only gave to parts of the code needed for a cancel button.

And that's pretty much it...very simple but effective way to create a cancel button
for the whole KnockOut form