Experimenting with Ruby in Umbraco – a MVC’ish macro

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

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=2.0.0.0, 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

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
http://our.umbraco.org/forum/developers/extending-umbraco/13698-IronRuby-and-Umbraco
and
http://ironruby-rocks.com/

Partial rendering and Ajax

A cool thing about this it that’s is not hard at all to add alternative rendering for Ajax-calls. We would do that by adding javascript to the get-view for a ajax post. And then we’d add an ajax-option to our controller. And thirdly we would have the controller be runnable without it’s full template context. For that reason we could use a empty template with the macro (which is what the Umbraco rndr package does).

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>

Happy coding!

Advertisements

3 thoughts on “Experimenting with Ruby in Umbraco – a MVC’ish macro

  1. Hi,
    Great post. I have played with IronRuby and Umbraco myself. I ran into a annoying problem, maybe you can help me?

    I cannot retrieve controller parameters inside my IronRuby script. I have one parameter with an alias named Headline.

    In my IronRuby script i’ve tried

    puts $Headline

    and

    puts @Headline

    Both returning nil

    Any idea how to retrieve this inside IronRuby (in Umbraco)?

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s