Saturday, December 11, 2010

Ruby Challenge 2010: Creating first application and working with Git

Hi all one more time,

Last time we stopped by installing Rails. Let’s check one more time it’s current version

C:\Users\Monomachus>rails --version
Rails 3.0.3

You can notice that from the last time we talked about it Rails version changed from 3.0.1 to 3.0.3. That’s because I’ve updated my system.

Now lets create a new application with Rails. We can do it by using “rails new name_of_the_application” command

D:\rails_tutorial_project>rails new first_application
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/application.js
      create  public/javascripts/controls.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/effects.js
      create  public/javascripts/prototype.js
      create  public/javascripts/rails.js
      create  script
      create  script/rails
      create  test
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep

Will get to each of those files in this series posts, this is what I hope at least to achieve.

Tip of the day: .gitignore file is used to let git know what kind of files we don’t want to add/commit to repository 

Ok, so we got everything created now and rails made for us a brand new application which should already work so let’s try it with “rails server” command. First we need to cd into the new created directory.

And I got error at first

railsStartError

And these errors in command line

D:\rails_tutorial_project\first_application>rails server
C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.2-x86-mingw32/lib/sq
lite3.rb:6:in `require': no such file to load -- sqlite3/sqlite3_native (LoadErr
or)
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.2-x86-m
ingw32/lib/sqlite3.rb:6:in `rescue in <top (required)>'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/sqlite3-ruby-1.3.2-x86-m
ingw32/lib/sqlite3.rb:2:in `<top (required)>'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:64:in `require'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:64:in `block (2 levels) in require'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:62:in `each'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:62:in `block in require'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:51:in `each'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r/runtime.rb:51:in `require'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/bundler-1.0.7/lib/bundle
r.rb:112:in `require'
        from D:/rails_tutorial_project/first_application/config/application.rb:7
:in `<top (required)>'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:28:in `require'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:28:in `block in <top (required)>'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:27:in `tap'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:27:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

After poking a little bit on forums I saw that I need to download the latest version of Sqlite dll’s and put them into my “ruby\bin” folder. Ok no problems, let’s do it. So you can download it here.

sqliteDownloads

Downloaded, unziped, xcopied into ruby\bin folder. Let’s try again.

D:\rails_tutorial_project\first_application>rails server
=> Booting WEBrick
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2010-12-11 10:00:40] INFO  WEBrick 1.3.1
[2010-12-11 10:00:40] INFO  ruby 1.9.2 (2010-08-18) [i386-mingw32]
[2010-12-11 10:00:44] INFO  WEBrick::HTTPServer#start: pid=3560 port=3000

Ok so now that works fine.

runningServer

P.S. Anyone knows how to run the server and then to detach in windows it also gives me an error in Windows when I try to use “rails server –d”

D:\rails_tutorial_project\first_application>rails server -d
=> Booting WEBrick
=> Rails 3.0.3 application starting in development on http://0.0.0.0:3000
C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.3/lib/active_suppor
t/core_ext/process/daemon.rb:3:in `fork': fork() function is unimplemented on th
is machine (NotImplementedError)
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/activesupport-3.0.3/lib/
active_support/core_ext/process/daemon.rb:3:in `daemon'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/serv
er.rb:262:in `daemonize_app'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/serv
er.rb:202:in `start'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands/server.rb:65:in `start'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:30:in `block in <top (required)>'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:27:in `tap'
        from C:/rubies/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails
/commands.rb:27:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'
WORKING WITH GIT

Ok now let’s try to work a little bit with git. I’ll be using now the “Git Bash” command line.

$ git init                // initilize the repository
$ git add .               // adds everything to repository
$ git commit -m "First commit" // make initial commit

So here everything should be clear. In git commit command –m means specifying message.

Now we can check the logs

Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ git log
commit 24d181eef79f1c6bd28898bcb0a587ff04a73dca
Author: unknown <Monomachus@.(none)>
Date:   Sat Dec 11 10:12:55 2010 +0200

    First commit

Now let’s see how can we checkout but first let’s delete some important file (Gemfile for example) and verify the status of git.
Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ rm Gemfile*

Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ ls
README    app     config.ru  doc  log     script  tmp
Rakefile  config  db         lib  public  test    vendor

Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    Gemfile
#       deleted:    Gemfile.lock
#
no changes added to commit (use "git add" and/or "git commit -a")

Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ git checkout -f

Monomachus@MONOMACHUS-PC /d/rails_tutorial_project/first_application (master)
$ ls
Gemfile       README    app     config.ru  doc  log     script  tmp
Gemfile.lock  Rakefile  config  db         lib  public  test    vendor

 

So you can see here that using “git status” command we checked out the status of repository and with “git checkout –f” command we made a forced checkout and restored everything as it was before.

It’s all for today. Next time we’ll play a little with Github and Heroku. I hope you liked it.