Learn to build a Rails app using Heroku and Git

Hi All:

In 2009, I discovered Heroku service for building applications. Not long time ago was bought by Salesforce and it has expanded its list of programming languages including other languages other than Ruby. It is extremely useful and extremely easy to use. It is also based in Git a SCM system which is also revolutionazing the version control systems.

To install Git, we could use this guide located on the progit or also you can see this page which explains how to use textmate with git

We set git configuration:

<span>$</span> git config --global user.name <span>"Your Name"</span>
<span>$</span> git config --global user.email youremail@example.com

There is a great Quickstart for using Heroku which I used as a base for this post: http://docs.heroku.com/quickstart

  • Sign in in Heroku
  • Install Heroku gem:
$ sudo gem install heroku
  • Create the local application in your mac: rails myapp
  • Access to the rails app: cd myapp
  • Install git
  • Execute git:  $ git init
  • Do git add:  $ git add .
  • We access to the following command:
    • Do git first commit: $ git commit -m "new app"
    • We open the application, and we start the local server ruby script/server and access to localserver using http://127.0.0.1:3000
    • As usual, we generate a index or home controller. I generally call them dashboard as it is sexy: $ ruby script/generate controller dashboard index
    • We delete the entry page: $ rm public/index.html
    • We modify the config/routes.rb to enter the following route map:
      • We create the heroku application directly on the heroku site from our computer merely using the Heroku’s gem
      • Now the web site will be visible on the following address: http://severe-stone-45.heroku.com. Sustitute the name by the name provided by Heroku and you got it. As the heroku repository is blank when we create the application we need to deploy the code to a specific branch, we deploy the code throuh this instruction
    • $ rake db:create
      db/development.sqlite3 already exists
      map.connect ':controller/:action/:id'
      map.connect ':controller/:action/:id.:format'
      # Enter the route to access to enter the dashboard
       map.root :controller =&gt; "dashboard"
      rake db:migrate
      heroku create
      Created http://severe-stone-45.heroku.com/ | git@heroku.com:severe-stone-45.git

      $ git push heroku master

    Create a LiftWeb project with Simple Build Tool

    If you are like me an eternal beginner on the Lift Web Framework, after starting a few attempts you probably think that perhaps maven lift archetypes are sometimes really slow process to start a liftweb project, and also that you need something more intuitive as Rails or Grails. Most of the time, when I am using maven, I need to look on the documentation again and again. So I have decided that I would like to use other option such as the Simple Build Tool.

    First, we are going to install sbt. I have already made a small tutorial of how to install sbt for Mac OS X Leopard users, which is located here

    Once the SBT tool has finally been installed, we proceed to create the lift project structure, there are 2 ways to create a Lift Project, the first one to create a new project by the traditional means using maven:

      mvn archetype:generate \
               -DarchetypeGroupId=net.liftweb \
               -DarchetypeArtifactId=lift-archetype-basic_2.8.0 \
               -DarchetypeVersion=2.1                           \
               -DarchetypeRepository=http://scala-tools.org/repo-snapshots \
               -DremoteRepositories=http://scala-tools.org/repo-snapshots \
               -DgroupId=org.activoricordi.cloudconsole \
               -DartifactId=dbcloudconsole \
               -Dversion=1.0

    Then use sbt to manage the rest. We later build the sbt project. However, this way of working is precisely what I want to avoid.

    The other option is to use git and download the lift-sbt-prototype from the LiftWeb Prototype Site. If you have not downloaded git, I would suggest two options or either find at Google a good tutorial of how to do it or the other option is to wait for new tutorial where we explain how we install it in Eclipse.

    We clone the project repository using the following command:

    git clone git://github.com/lift/lift_sbt_prototype.git

    But there is a better option, which is using Lifty. Lifty is a SBT processor for generating files associated with the Lift framework. It is a open source project created by Mads Hartmann, which is based on a sbt framework called Lifty engine, which we will describe another day.

    Lifty site

    Lifty site

    mkdir lifty_test
    cd lifty_test
    sbt

    Once we enter the sbt command the following message will appear:

    Project does not exist, create new project? (y/N) : y

    SBT Project Creation Line


    If we select the option Y and we then to enter commands to fill the parameters appear:

    • Name:liftytest
    • Organization:org.lifty
    • Version [1.0]:1
    • Scala version [2.7.7]: 2.8.0
    • sbt version [0.7.4]: 0.7.4
    • Here we can see an screenshot of the process:

      sbt_project_creation_process

      Screenshot from sbt project creation process


      This means that we have created a normal scala project, where we have filled the necessary fields
      We then enter the following command at the sbt prompt which are located on this page: http://lifty.github.com/Lifty/:

      *lifty is org.lifty lifty 1.3
      

      This command download the lifty processor.

      To test it, we are going to enter the following command.

      lifty help
      

      The lifty processor is quite simple, supporting the following commands:

      help: This will list all of the available commands
      templates: This will list all of the available templates
      create: This will attempt to create on of the templates

      lifty create project
      

      This command creates all the structure needed to execute the project. Even it creates the command.

      import sbt._
       
      class LiftProject(info: ProjectInfo) extends DefaultWebProject(info) {
        val mavenLocal = "Local Maven Repository" at
        "file://"+Path.userHome+"/.m2/repository"
       
         val scalatoolsSnapshot = "Scala Tools Snapshot" at
        "http://scala-tools.org/repo-snapshots/"
       
        val scalatoolsRelease = "Scala Tools Snapshot" at
        "http://scala-tools.org/repo-releases/"
       
        val liftVersion = "2.1-SNAPSHOT"
       
        override def libraryDependencies = Set(
          "net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",
          "net.liftweb" %% "lift-testkit" % liftVersion % "compile->default",
          "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default",
          "net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",
          "com.h2database" % "h2" % "1.2.138",
          "org.mortbay.jetty" % "jetty" % "6.1.22" % "test->default",
          "junit" % "junit" % "4.5" % "test->default",
          "org.scala-tools.testing" % "specs" % "1.6.1" % "test->default"
        ) ++ super.libraryDependencies
      }

      As a tip, I would recommend adding are going to add the following dependencies which I consider very valuable.

      "net.liftweb" %% "lift-widgets" % liftVersion % "compile->default",

      Once we add it we have to save the file and use the sbt reload command again.
      To summarize:
      a) We create the folder with the name of the project we want to create
      b) We access to the folder and we then use the sbt command
      c) Instead of filling it

    Install the Simple Build Tool for LiftWeb Framework

    Install the Simple Build Tool (SBT) for LiftWeb Framework

    Best way to install SBT for Mac OS X is described on the following tutorial called Getting Started with Scala using SBT.
    However, in this case since I do not have wget installed, we have downloaded the sbt-launcher.jar file from the official Simple Build Tool and place it on our Downloads folder. Using the following commands, we can install the sbt launcher and make it accessible from the command tools :

    cd ~
    sudo mv ~/Downloads/sbt-launch-0.7.4.jar /usr/local/bin/sbt-launcher.jar
    echo "java -Xmx512M -jar /usr/local/bin/sbt-launcher.jar \"\$@\"" | sudo tee /usr/local/bin/sbt
    sudo chmod +x /usr/local/bin/sbt

    Thank’s to Chris Moos script, the SBT jar is installed and it creates a script called sbt that will allow us to run the sbt jar. To test it just type sbt and press enter, and you now have access to SBT.

    Create a Lift Project with Maven

    Install Lift 2.1 in MacOSX Leopard > (4) Create a Lift Project with Maven:

    The Apache Maven has become a standard, a standard that is already installed in Mac OS X Leopard by default, and there is no need to installed again. It is also possible to update it.

    Once the maven is installed, we can use it to create a Lift Project, you could start the mvn command, and create a project with the following structure.

    mvn archetype:generate \
     -DarchetypeGroupId=net.liftweb \
     -DarchetypeArtifactId=lift-archetype-basic_2.8.0 \
     -DarchetypeVersion=2.1 \
     -DarchetypeRepository=http://scala-tools.org/repo-releases \
     -DremoteRepositories=http://scala-tools.org/repo-releases \
     -DgroupId=org.activoricordi.app \
     -DartifactId=lift_app_test \
     -Dversion=1.0

    We are going to analyze each of the terms on this archetype:

    • archetype:create -U: It indicates the type of archetype we are going to use, there could be others which we will use to build the application or deployed
    • -DarchetypeArtifactId can have one of the three values
      • - lift-archetype-blank: To create a blank project without any database or ORM.
      • - lift-archetype-basic:To create a liftweb project with ORM and derby database, it is good for prototyping a project and help to start with beginners
      • - lift-archetype-jpa-basic To create a liftweb project with ORM and jpa database

      We modify the following parameters to adjust it to our project:

      -DgroupId=org.activoricordi.app \
       -DartifactId=lift_app_test \
       -Dversion=1.0

      Once the execution of the Maven is completed, we execute the following commands:

    Install Apache Maven in Mac OS X Leopard

    Install Lift 2.1 in MacOSX Leopard > (3) Install Maven in MacOSX Leopard:

    As we said before, Lift project uses Apache Maven as its default build system, luckily Maven is already installed on the Mac OS X Leopard computers, and we can check the version using the following command:

    mvn --version

    It is will appear the following message:

    Apache Maven 2.2.0 (r788681; 2009-06-26 15:04:01+0200)
    Java version: 1.6.0_20
    Java home: /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
    Default locale: en_US, platform encoding: MacRoman
    OS name: "mac os x" version: "10.6.4" arch: "x86_64" Family: "mac"

    If you don’t already have Maven or in case it does not have the version you are looking for, then, the first thing to do is to download and install it on your Mac OS X Leopard. The process of installing Maven typically involves four simple steps, which are as follows:

    1) To download a release build archive appropriate for your platform, in this case MacOS X from http://maven.apache.org/download.html. You may want to get the zip file on a Windows machine and tarred zip or bz2 file on a Linux, Unix or Mac OSX machine.

    2) Expand the archive file in a preferred folder within your file system.

    3) Create or update a few environment variables, namely:

    JAVA_HOME – point it to your JDK folder. I assume JDK is already installed. If not, go get it before you go further.
    M2_HOME – point it to the folder where Maven is installed. It’s the same that was created when you extracted the Maven archive.
    M2 – point it to %M2_HOME%\bin on windows and $M2_HOME/bin on Linux/Unix/Mac OSX
    PATH – update the PATH environment variable. Add M2 to it.
    MAVEN_OPTS – Add -Xms256m -Xmx512m to this environment variable.

    Install Lift 2.1 in MacOSX Leopard (II) – Install the Scala Language

    2) Install the Scala Language:

    I open the finder and choose the option Go to Folder select /usr/local/, then we rename the folder to Scala and we copied the folder there.We could also move it by Terminal using the following command:

    $ sudo mv ~/Downloads/scala-2.8.0.final /usr/local
    $ sudo mv ~/Downloads/scala /usr/local

    if we have already changed its file name.We edit the local PATH file. We open the Terminal application, we edit the .profile file (we can use vim editor or the TextEdit, or directly use Textmate if we have it installed using the mate ~/.profile).

    open -a TextEdit .profile
    SCALA_HOME=/usr/local/scala;
    export $SCALA_HOME; export SCALA="$SCALA_HOME/bin/scala"
    PATH=$SCALA_HOME/bin:$PATH; export PATH
    source ~/.profile

    To test it we can insert the following command on the Terminal application

    scala -version

    And the following sentence will appear:

    Scala code runner version 2.8.0.final -- Copyright 2002-2010, LAMP/EPFL

    Install Lift 2.1 in MacOSX Leopard (I) – Download the Scala Language

    1) Download the Scala Language:

    We download the Scala language file from the scala-lang site. We are going to install it following the instructions given by the following sites Installing Scala on Mac OS X Leopard and How to Install Scala on Mac. I have done a quick summary of the great tips:

    • We go the official Scala Web Site, http://www.scala-lang.org/downloads.
    • Download the installer for your environment and follow the instructions on the downloads page.
    • I download the scala-2.8.0.final.tgz, I extract it in the Downloads folder.

    Install Lift Web Framework – The Series

    If you are a loyal reader of this blog, you know that I am a rather new fan of Lift and Scala. I have recently move to use Lift 2.1 over Scala 2.8.0.  Unfortunately, I cannot work with Lift as much as I want. My story with Lift and Scala is fill with a lot of blank spaces where, due to my job, I have no contact with Lift coding at all. For that reason, I have created these set of Quick and dirty instructions as tutorial where I review tips, tricks to install Scala, Lift Web Framework, Simple Build Tool, Maven, and Eclipse.

    The idea is to keep it updated from time to time including my new findings, and novelties as I believe it is going to change at lot in the very next future. Although I initially envision this as a simple post, I have decided to make a series in order to maintain them more easily and keep contributing to the series after.

    Install Lift 2.1 in Mac OS X Leopard I – Download the Scala Language
    Install Lift 2.1 in MacOSX Leopard (II) – Install the Scala LanguageShortlink
    Install Lift 2.1 in Mac OS X Leopard I – Download the Scala Language
    Install Lift 2.1 in Mac OS X Leopard I – Download the Scala Language
    Install Lift 2.1 in Mac OS X Leopard I – Download the Scala Language
    Install Lift 2.1 in Mac OS X Leopard V – Download the Scala Language
    Install Lift 2.1 in Mac OS X Leopard VI – Download the Scala Language
    Install Lift 2.1 in Mac OS X Leopard VII – Download the Scala Language

    I have changed to JQuery in Rails

    jQuery is today the best js library, prototype is a challenger.The reasons why I think this are beautifully illustrated on the following post. To use jQuery in rails install the following plugin:

    ruby script/plugin install http://ennerchi.googlecode.com/svn/trunk/plugins/jrails

    Here is a list of existing functions which are available once we replace prototype+scriptacuolous for the jquery. Hopefully, this support will improve once we approach to Rails 3.

    If you want to use both libraries, you will keep prototype js files, if you plan to use only jquery you could removed them. I particularly think that seems you have decided to use jquery there is few reasons to keep them.

    Also, there are two ways to use jquery, I just found this interesting article which speak about a different way to use jquery, not using jrails but instead in a more unobstrusive way. Interesting point of view.

    http://www.notgeeklycorrect.com/english/2009/05/18/beginners-guide-to-jquery-ruby-on-rails/

    Kind regards and good weekend

    JRuby on Rails on SAP NetWeaver Studio

    Many of you have already heard about JRuby and its Rails implementation called JRuby on Rails, for those who do not I would recommend to read the following articles:

    • JRuby: The power of Java and Ruby
    • This article which clearly explain the basics is about JRuby.
    • This article from Java World gives an overview JRuby on Rails. 
    • Additionally, there is another article from Developer.com which runs over the first steps on creating and deploying a JRuby on Rails application.

    Ok, once we are all on the same page, I am going to make a more detailed overview, on how to develop a JRuby on Rails project using SAP NetWeaver Studio. Just to mention that I am currently using version 7.1 SP6 which is built over Eclipse 3.3.0 and Java 5 Sdk.  I know it is a little bit more advanced than the most mayority of current SAP projects, which are probably using SAP NetWeaver Studio 7.0 but well, it is what I have.

    There is an existing one-click installer from JRuby on Rails suitable for testing for a company called bitnami, which has a very interesting market proposition. I am myself I use RubyStack with no futher problems.
    However, I have done manually to fully understand the development architecture. So let se which are the steps to perform:

    1) Download JRuby: As first step, we are going to download latest version of JRuby from JRuby site. In this case is JRuby 1.1.4, which can be download from its repositories. Select what it suits for your operating system.

    While you wait it to download, if you like you can read something or you want watch a video related to JRuby on Rails, plese the following video:

    Ok, now it is already download, we have to find the file and unzip. Do not underestimate the time it takes to find the file wherever you have download it. :-) .

    As I always install my Jvm on the root directory of my C: drive (c:\jdk15,c:\jdk16,..) . I am going to follow the same procedure again, as it will ease the procedure of adding it to my PATH route. I do not know you but I am a little tired of adding things to my PATH, that I am not going to use so I have created a .bat file which do this for me. For more details about how to getting started. Obviously, it is designed to support my best practices, but you know today is “Convention over configuration” or DIY.

    Prerequisites

    JRuby 1.1.3 or higher
    Java 5 or higher
    Rails 2.0 or higher

     Install Rails Framework ‘gems install rails -y’ (if behind a http proxy, set HTTP_PROXY=http://${http-proxy-host}:${http-proxy-port}/)
    4. Install activerecord-jdbc ‘gems install activerecord-jdbc’
    5. Install database/jdbc driver (for mysql http://mysql.com/)
    6. Generate your Ruby on Rails application (http://rubyonrails.org/)
    7. Modify database.yaml
           development:
               adapter: jdbc
               driver: com.mysql.jdbc.Driver (for mysql)
               url: jdbc:mysql://${database-hostname}/${db-schema}
               username: ${username}
               password: ${password}

    8. Modify environment.rb by adding
           require ‘active_record/connection_adapters/jdbc_adapter’

    9. Download rails-integration-${version}-SNAPSHOT.jar into the WEB-INF/lib by checking out and building the rails-integration project
    svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration
    10. Modify the template web.xml(see references below) with the right value of jruby.home
    11. Create a WEB-INF directory in the RoR application directory
    12. Copy web.xml to WEB-INF and rails-integration-${version}-SNAPSHOT.jar under WEB-INF/lib
    13. Jar up the RoR application directory contents as a WAR file

    Requests to appropriate context-root of the deployed web application should invoke your Ruby code !

    If you’d like to automate the building of a war file:
    Edit rails-integration/build.xml and add the following XML snippet
      <property environment=”env”/>
      <target name=”build-rails-war” depends=”jar”>
        <delete file=”${rails-app-dir}/${rails-app-name}.war”/>
        <mkdir dir=”${rails-app-dir}/WEB-INF/lib”/>
        <copy todir=”${rails-app-dir}/WEB-INF/lib”>
          <fileset file=”${maven.build.directory}/${maven.build.final.name}.jar”/>
          <fileset file=”${maven.repo.local}/org/jruby/jruby/0.9.1/jruby-0.9.1.jar”/>
          <fileset file=”${maven.repo.local}/asm/asm/2.2.2/asm-2.2.2.jar”/>
          <fileset file=”${maven.repo.local}/javax/activation/activation/1.1/activation-1.1.jar”/>
        </copy>
        <copy todir=”${rails-app-dir}/WEB-INF/”>
          <fileset file=”samples/scaffold/WEB-INF/web.xml”/>
        </copy>
        <replace file=”${rails-app-dir}/WEB-INF/web.xml” token=”/usr/local/jruby” value=”${env.JRUBY_HOME}”/>
        <jar jarfile=”${rails-app-dir}/${rails-app-name}.war” basedir=”${rails-app-dir}”/>
      </target>
    Make sure JRUBY_HOME is set and run
    ‘ant -Drails-app-dir=${ror-app-dir} -Drails-app-name=${war-file-name} build-rails-war’
    ${ror-app-dir}/${ror-app-name}.war should be ready for deployment !

    jruby -S gem install -y rails warbler

    $JRUBY_HOME/bin/gem install activerecord-jdbc-adapter -y

    modify database.yml

    development:
    adapter: jdbc
    driver: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost/blabla_development
    username: root
    password: root

    Modify environment.rb

    require File.join(File.dirname(__FILE__), 'boot')
    if RUBY_PLATFORM =~ /java/
    require 'rubygems'
    RAILS_CONNECTION_ADAPTERS = %w(jdbc)
    end
    I would recommend to read of The empty Way
    JRuby entries on WordPress

    Rails::Initializer.run do |config|

    This article is based on the following references:

    Kind regards