My intention here is to show how easy it is to create a set of scripts in IronRuby with a nice controller / views separation and have them connected to a Umbraco macro. The macro (with Scripts) will show a simple html form with a submit button at first, and a thank you message after submit. (If you like to try this code yourself, add the root-path as I mention at the end.)
The controller is the Ruby script responsible for handling the user interaction. It is also the script that has the actual macro connected to it.
The Umbraco IronRuby implementation does not add System.Web assembly automatically, so we have to do a require with the full name and publickeytoken.
After that the test is straight forward and the code loads (and executes) the correct view. In a more real sample we would ofcourse have some code before the POST view load.
require 'System.Web, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' currentContext = System::Web::HttpContext.Current if currentContext.Request.RequestType=="POST" # handle POST (not implemented, we just choose a view to show afterwards) load 'test-view-after-post.rb' else load 'test-view-get.rb' end
If you try this yourself you will need to check the Skip testing (ignore errors) checkbox, otherwise it will complain about the missing files. Also have the create macro-checkbox checked when you create the Script file.
The views are two simple Ruby-files without connected macros.
test-view-get.rb creates a very basic html form for us:
puts "<form method='post' action='?'><input type='submit'/></form>"
And test-view-after-post.rb shows a thank you message:
puts 'Thank you for submitting'
There you go – a very simple way to create a Umbraco macro with a MVC touch.
Adding more advanced features in the views
As the views are Ruby-files we can add as much logic we like. For a really rich View-script I’d recommend using a templating library like Erb. I intend to write another post about that in a short while.
Meanwhile, have a look at
Partial rendering and Ajax
Cooler than Python?
Some things makes IronRuby easier to work with than IronPython in their current states in Umbraco at the moment IMHO:
The root path to the script files is easily configurable (by changing config-settings). It’s very easy to load and run another scriptfile. It’s easy to add a great templating library (Erb).
Adding the root path
To be able to call your rb-files (like in the code sample) easily you need to add the correct path to your scripts-folder. The folder is called \python (yes) and is in the root of every Umbraco installation. So open your /config/scripting.config file and add the path to python\; : But I find IronPython to be a better partner to .Net + I like the syntax a little bit more. Therefore I prefer it over IronRuby.
<microsoft.scripting> ... <options> <set language="Ruby" option="LibraryPaths" value="python\;... </options> </microsoft.scripting>