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 activerecord-sqlserver-adapter --no-rdoc --no-ri
2. Add the following to your app's config/environment.rb:
config.gem "activerecord-sqlserver-adapter", :version => ">= 2.3.5"
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. The .NET 4.0 MSI is suggested as this adds IronRuby to your path and ahead-of-time compiles IronRuby for your system, greatly improving performance.
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, dbd-adonet and dbd-adonet-sqlserver must be used with the dbi gem. To do all that, just install the "activerecord-adonet-sqlserver" gem:
> igem install activerecord-sqlserver-adapter --no-rdoc --no-ri
Successfully installed activerecord-sqlserver-adapter-2.3.5
1 gem installed
Make sure to use the "--no-rdoc" and "--no-ri" flags. Otherwise, you can get an error while generating the RI documentation. If you do get the error, you can still use the gem as only the documentation is affected. http://github.com/rails-sqlserver/2000-2005-adapter/issues/issue/30 is the bug for this issue.
SQLite3 (Community.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.
> ir -S gem install sqlite3-ironruby --no-ri --no-rdoc
Successfully installed sqlite3-ironruby-0.1.1
New Project
To create a new Rails project with IronRuby:
> ir -S rails 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
Database
Add a following the config/environment.rb inside the block, which will load the "activerecord-sqlserver-adapter" gem, setting up ActiveRecord to work with SQLServer:
config.gem "activerecord-sqlserver-adapter", :version => ">= 2.3.5"
For Sqlite3 add the following to your environment.rb:
config.gem "sqlite3-ironruby", :lib => "sqlite3"
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.
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.
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.
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
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.
> ir -S rake 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 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
POST /posts/create and redirect to GET /posts
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">]