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:
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:
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.
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:
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"):
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
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
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
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.
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">]