Rails

From $1

Walk-throughSetup | New Project | Database | Web Server (production) | Scaffolding | Migrations | Requests | Console

Quick Steps

For the impatient, here are the quick-steps if you already know how to use Rails and SQLServer:

1. Install Rake, Rails, and IronRuby SQLServer

igem install rake rails ironruby-sqlserver

2. Add the following to the top of your app's config/environment.rb:

require 'rubygems' 
require 'ironruby_sqlserver'

3. Connect to SQLServer in config/database.yml:

development:
  mode: ADONET
  adapter: sqlserver
  host: YOURMACHINENAME\SQLEXPRESS
  database: app123_development
  integrated_security: true

Walk-through

For the beginner or more-curious, the following walks through setting up IronRuby, Rails, and SQLServer, and shows a basic Rails "scaffold" running.

Setup

1. Download latest version of IronRuby

Make sure you install into a path with no spaces (for example: C:\IronRuby). Also add IronRuby's binaries to your path for easy command-line access:

> set PATH=%PATH%;C:\IronRuby\bin;C:\IronRuby\lib\IronRuby\gems\1.8\bin

2. Install Rails and Rake with RubyGems

RubyGems is a standard way to install Ruby libraries, install the Rake and Rails libraries:

> igem install rake rails --no-rdoc --no-ri
Successfully installed rake-0.8.7
Successfully installed activesupport-2.3.5
Successfully installed activerecord-2.3.5
Successfully installed rack-1.0.1
Successfully installed actionpack-2.3.5
Successfully installed actionmailer-2.3.5
Successfully installed activeresource-2.3.5
Successfully installed rails-2.3.5
8 gems installed

Note: The flags passed to igem install avoid generating local documentation the library, which speed up installation. Remove the flags if you want local documentation. Also, if you're behind a firewall, the above commands probably failed for you; setup an environment variable for your proxy server: set HTTP_PROXY=http://your.proxy.com:1234

3. Install a Database

Most web applications interact with a database; here are the databases that IronRuby supports:

SQL Server Express(free download)

While it is recommended to use this database for development, if you can use SQLServer for production, though Express can be also used in production for smaller websites. If you wish to use this, then you'll need to install the SQL Server ActiveRecord adapter: this adapter is the official SQL Server for Rails. For usage with IronRuby, ironruby-dbi must be used instead of Ruby's built-in DBI library. To do all that, just install the "ironruby-sqlserver" gem:

> igem install ironruby-sqlserver --no-rdoc --no-ri
Successfully installed ironruby-dbi-0.1.0
Successfully installed activerecord-sqlserver-adapter-2.3.0
Successfully installed ironruby-sqlserver-0.1.0
3 gems installed

SQLite3 (System.Data.Sqlite.dll)

SQLite3 is a popular development database, and System.Data.Sqlite.dll is a repackaging of SQLite3, adding a ADO.NET provider so .NET can use SQLite3 as well. While IronRuby can directly use SQLite3 this way, the ActiveRecord SQLite3 adapter has not been tested with IronRuby yet. TODO test the ActiveRecord SQLite3 adapter with IronRuby, and provide setup instructions here.

TODO: any other databases supported?

New Project

To create a new Rails project with IronRuby:

> irails IronRubyOnRails
      create
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
      create  config/initializers
      create  config/locales
      create  db
      create  doc
      create  lib
      create  lib/tasks
      create  log
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  script/performance
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance
      create  test/unit
      create  vendor
      create  vendor/plugins
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  Rakefile
      create  README
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  config/database.yml
      create  config/routes.rb
      create  config/locales/en.yml
      create  db/seeds.rb
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/new_rails_defaults.rb
      create  config/initializers/session_store.rb
      create  config/environment.rb
      create  config/boot.rb
      create  config/environments/production.rb
      create  config/environments/development.rb
      create  config/environments/test.rb
      create  script/about
      create  script/console
      create  script/dbconsole
      create  script/destroy
      create  script/generate
      create  script/runner
      create  script/server
      create  script/plugin
      create  script/performance/benchmarker
      create  script/performance/profiler
      create  test/test_helper.rb
      create  test/performance/browsing_test.rb
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/index.html
      create  public/favicon.ico
      create  public/robots.txt
      create  public/images/rails.png
      create  public/javascripts/prototype.js
      create  public/javascripts/effects.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/controls.js
      create  public/javascripts/application.js
      create  doc/README_FOR_APP
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log

Note: in future versions of IronRuby, the only "i" scripts in C:\IronRuby\bin will be iirb and igem, so either use rails.bat or ir -S rails; the latter is preferred as it ensures IronRuby is used, as the former depends on which rails.bat is first found on the path.

Database

Add a following to the top of config/environment.rb, which will load the "ironruby-sqlserver" gem, setting up ActiveRecord and the underlying ruby-dbi library to work with IronRuby:


require 'rubygems'
require 'ironruby_sqlserver'
view raw ironruby.rb This Gist brought to you by GitHub.

The rails command will generate a config/database.yml file for sqlite3, so it needs to be changed for sqlserver (make sure to replace "YOURMACHINENAME"):

development:
  mode: ADONET
  adapter: sqlserver
  host: YOURMACHINENAME\SQLEXPRESS
  database: ironruby_on_rails_dev
  integrated_security: true

Now create the ironruby_on_rails_dev database on your machine. If you have Visual Studio 2008 (Express is fine), you can use "Tools -> Connect to Database" to connect to the database server and create the database. Otherwise, you can download SQL Server 2008 Management Studio Express and do the same thing.

rails-server-explorer.PNG

Web Server

Now that we have a Rails project, let's run it. The Ruby standard library contains WEBrick, a Ruby web-server, which can be used to serve Rails projects for development purposes. To run the Rails project:

> cd IronRubyOnRails
> ir script\server
=> Booting WEBrick
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2009-04-22 13:55:50] INFO  WEBrick 1.3.1
[2009-04-22 13:55:50] INFO  ruby 1.8.6 (2009-03-31) [i386-mswin32]
[2009-04-22 13:55:50] INFO  WEBrick::HTTPServer#start: pid=10848 port=3000

Open a browser to http://localhost:3000, and you will see the Rails welcome page.

rails-welcome.png

However, this just serves public/index.html with WEBrick, but does not exercised any of Rails. Clicking the About your application's environment link will cause Rails to process the Rails::InfoController#properties controller action, which pokes the database, and render a view.

rails-aboutenv.PNG

The command window running Rails will now show:

Processing Rails::InfoController#properties (for 127.0.0.1 at 2009-04-22 14:20:50) [GET]
Completed in 152ms (View: 128, DB: 0) | 200 OK [http://localhost/rails/info/properties]

Note: this action is not avaliable in production mode

Production

IronRuby can also run Rails on IIS, and is the preferred way of running production applications. See IronRuby.Rack for more information.

Note: there is no binary release of IronRuby.Rack yet, though there will after IronRuby 1.0 RC2.

Scaffolding

To quickly show IronRuby running the entire Rails stack (ActiveRecord, ActionController, and ActionView), generate a scaffold:

Note: you can keep WEBrick running and do this in a new command prompt to avoid restarting Rails

> ir script\generate scaffold post title:string body:text published:boolean
      create  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/posts
      exists  app/views/layouts/
      exists  test/functional/
      create  test/unit/
      create  test/unit/helpers/
      exists  public/stylesheets/
      create  app/views/posts/index.html.erb
      create  app/views/posts/show.html.erb
      create  app/views/posts/new.html.erb
      create  app/views/posts/edit.html.erb
      create  app/views/layouts/posts.html.erb
      create  public/stylesheets/scaffold.css
      create  app/controllers/posts_controller.rb
      create  test/functional/posts_controller_test.rb
      create  app/helpers/posts_helper.rb
      create  test/unit/helpers/posts_helper_test.rb
       route  map.resources :posts
  dependency  model
      exists    app/models/
      exists    test/unit/
      create    test/fixtures/
      create    app/models/post.rb
      create    test/unit/post_test.rb
      create    test/fixtures/posts.yml
      exists    db/migrate
      create    db/migrate/20090422182202_create_posts.rb

Migrations

The scaffold command above generated a migration in db/migrate/<timestamp>_create_posts.rb to create a "posts" table with "title", "body", and "published" fields. Run the migration to commit it to the database.

> irake db:migrate
(in C:/dev/IronRubyOnRails)
==  CreatePosts: migrating ====================================================
-- create_table(:posts)
   -> 0.0420s
==  CreatePosts: migrated (0.0530s) ===========================================

Requests

With the database set up properly, the scaffold can be used. Visit http://localhost:3000/posts in your browser and you will see the generated page.

GET /posts

rails-scaffold-index (1).PNG

Rails is processing the request for GET /posts, routing it to the PostsController#index method, fetching all rows out of the posts table, and rendering them with an ERb view. It can also process POST requests which manipulate the database:

GET /posts/new

ir-posts-new.PNG

POST /posts/create and redirect to GET /posts

ir-posts-list-data.PNG

Console

The "script/console" command opens an irb session pre-configured against your Rails environment.

> ir script\console --irb="C:\IronRuby\bin\iirb.bat"
Loading development environment (Rails 2.3.5)
>> Post.all
=> [#<Post id: 1, title: "IronRuby on Rails", body: "First post showing that IronRuby runs Rails!", 
      published: false, created_at: "2009-05-12 19:13:04", updated_at: "2009-05-12 19:13:04">]
>> p = Post.first
=> #<Post id: 1, title: "IronRuby on Rails", body: "First post showing that IronRuby runs Rails!", 
     published: false, created_at: "2009-05-12 19:13:04", updated_at: "2009-05-12 19:13:04">
>> p.published = true
=> true
>> p.save
=> true
>> Post.all
=> [#<Post id: 1, title: "IronRuby on Rails", body: "First post showing that IronRuby runs Rails!", 
      published: true, created_at: "2009-05-12 19:13:04", updated_at: "2009-05-12 19:42:39">]
Tags:
 
Images (6)
Viewing 1 - 6 of 6 images | View All
/posts/new
/posts/new
ir-posts-...  Actions
No description
ir-posts-...  Actions
Visual Studio Server Explorer - Rails
Visual Studio Server Explorer - Rails
rails-ser...  Actions
Rails Scaffolding - Index
Rails Scaffolding - Index
rails-sca...  Actions
No description
rails-abo...  Actions
Rails Welcome Page
Rails Welcome Page
rails-wel...  Actions
Comments (0)
You must login to post a comment.

 
SourceForge.net