An ERB Safemode handler for ActionView

posted: April 22nd, 2008 · by: Sven

in: Programming · tagged as: , , , , ·  8 comments »

Just some quick notes about the safemode library I’ve been working on with the help of Peter Cooper recently. Rather than starting out with a Haml specific library Peter suggested turning this into a more widely usable tool and hacked his way to make it eat plain Ruby code as well as ERB.

Since I’ve cleaned up things a bit and started working on a Rails ActionView ERB handler so one could transparently use this library when rendering ERB templates with ActionView. Yesterday I’ve managed to render a blog index page (which I used as a sample app) through this handler for the first time.

You can try things out like this:

  1. create a new Rails application
  2. install the plugin:
  3. add a controller, model and a view, like UserController, User and index.html.erb

For example, these would work:

# controllers/users_controller.rb
class UserController < ApplicationController
  def index
    @user ='Sven')

# models/user.rb
class User
  attr_reader :name
  def initialize(name)
    @name = name

# views/user/index.rb
<p><%= %></p>

At this point, this is just plain ERB rendered like in any other Rails application. You should see the user name output when you render /user.

Now, with the ERB safemode handler we can restrict what users can access in their ERB templates in a whitelisted manner. You can turn it on by registering the handler to ActionView:

# config/initializers/erb_safemode.rb
ActionView::Base.register_default_template_handler :erb,

When you now try to render /user you’ll see an empty page and an exception thrown on the console that says: undefined method ‘name’ for Safemode::Jail (User).

From that you can see that the user object has been wrapped into a Safemode::Jail proxy which restricts the access to the methods on the object. To allow the user to access the name method you can add this to the User model:

class User
  class Jail < Safemode::Jail
    allow :name

You should also be able to wrap that into a layout and use Rails helper methods:

# layouts/user.html.erb
<%= stylesheet_link_tag 'default' %>
<%= yield %>

What’s missing

The one issue with this that I just could not get my head wrapped around yet is the borked error handling.

If you’ve followed the steps above you’ve already seen that right now the template handler rescues exceptions itself and just prints them to the console (see the handler code here).

That, of course, is not too nice. If I remove that rescue block from there, though, and an exception gets raised (e.g. remove the Jail from the User model) then I’m getting no response from the server at all. Not a blank page, just no response.

curl -I http://localhost:3000/user

… just hangs and says “(7) couldn’t connect to host” after a while. Safari tells me the same while the log only states that is has started “Rendering user/index”, but ths usual execution time summary (“Completed in …”) is missing.


My first idea was that Rails tried to use the safemode handler to render the error page and ran into another error in that stage. So I tried to register the handler for *.serb.html and renamed the views … same result.

If you happen to have an idea how to fix this that would be very appreciated! :)

Leave a comment


  1. max said December 5th, 2008 at 10:35 AM  

    I had the same problem and I couldn’t fix it :(

  2. goodwill said November 11th, 2009 at 05:58 PM  

    This thing is kinda interesting- are you still working on this? I am using liquid… you know… that sucks…

  3. jack said January 23rd, 2011 at 10:41 AM  

    I’d really love to get some tests that demonstrate the reloading Dependencies problems. Got any ideas? cheap vps

  4. ddsgd said March 21st, 2011 at 06:28 AM  

    How to Convert AVCHD films to AVI

    URL? Troubles we may possibly have encountered to take satisfaction in and reveal AVCHD films with friends:

    1. You have shot lots of films utilizing AVCHD camcorder, and you also need to upload these films to internet that consist of Youtube to reveal with friends, but you can’t determine the right way to create it.
    2. you could possibly also uncover which you would not possess the ability to available your AVCHD camcorder films on house windows film maker or advertising player.

    That’s true, the AVCHD structure is not accepted by lots of players, and also this kind of structure cannot be uploaded to internet which prevents film sharing. I’ve searched near to some great offer and uncover an simplest alternative finally. The simplest way can be to convert avchd to avi or other standard formats.

    The alternative we are able to consider to solve them: An AVCHD to AVI Converter is needed. Please click to obtain avchd to avi converter, set up and run it. Step one Get AVCHD films from AVCHD Camcorder to PC

    Connect the AVCHD Camcorder for the PC using a USB cable. when attached and powered on, the camcorder should seek out the desktop like a brand brand new disk. It is desirable which you duplicate the films for the PC’s hard-drive earlier to converting avchd to avi or editing it. Step 2. fill AVCHD camcorder videos

    Click “Add Video” or just drag & squirrel away your AVCHD camcorder films using the document checklist directly, you can very easily include AVCHD camcorder videos.

    Load AVCHD camcorder videos Step 3. find AVI as output format

    Select AVI as output structure away from your categorized output formats checklist in “Profile” drop-down checklist and specify the output list to preserve your converted records in “Output” drop-down list.

    Select Convert AVCHD films to AVI output format Step 4. start to convert avchd to avi

    After every little thing is done, strike “Start” key to convert avchd to avi. And all the things can be achieved at quickly demand and higher quality. The conversion demand often is dependent for the genuine film sizing as well as your PC configuration. as well as the output best quality is great.

    After converting avchd to avi, now you can upload these AVI films to internet or view them with house windows advertising Player/Movie Maker to reveal them jointly with your friends. The complete method is easy, and you also can possess a try of the AVCHD to AVI Converter to how to convert avchd to avi and reveal your delighted instant easily.

    Tips: Why we choose AVI format? As we realize that AVI structure is among the probably the most standard formats, that is broadly accepted by the majority of players. as well as the best quality of AVI film is great. using the sake of reputation and compatibility, AVI is regarded as probably the most advantageous choice. Therefore, you only should convert AVCHD to AVI, after which you can very easily reveal your AVCHD camcorder videos.

  5. Cams said March 30th, 2011 at 07:43 PM  

    I’m still having problems with liquid. I hope there is a fix soon.

  6. Okey oyunu said May 12th, 2011 at 03:38 PM  

    Tüm dünya artik okey oyunu oynuyor. Yillardir bir çok oyun programi olmasina ragmen, içlerinden en güzeli olarak nitelendirebilecegimiz tek bir site göze çarpmaktadir. Diger tüm okey oyunu programlarinin aksine ücretsiz olmasi ve 3 boyutlu olarak hizmet vermesi mükemmel bir gelismedir. Sizlerde www. adresinden bu essiz okey oyununu indirebilirsiniz. Kullanimi çok basit ve Türkçe dil seçenegi ile kolaylikla oyuna baslayabilirsiniz. Ister kendi ülkenizden, isterseniz dünyanin tüm farkli bölgelerinden dilediginiz oyun odalarini seçerek, oyuna hemen baslayabilirsiniz. Okey oyunu oynamak için artik arkadas bile aramaniza gerek kalmadan, bilgisayarinizdan 100 binlerce üye ile online olarak okey oyununu oynamanin zevkine varabilirsiniz.

  7. porno said May 23rd, 2011 at 10:44 AM  

    good comment. thanks you friends.

    I’ve surfed the net more than three hours today, however, I haven’t found such useful information. Thanks a lot, it is really useful to me

  8. porno said May 23rd, 2011 at 10:45 AM  

    I do agree with all of the ideas you have presented in your post. They’re really convincing and will definitely work. Still, the posts are too short for newbies. Could you please extend them a bit from next time? Thanks for the post.

Sorry, comments are closed for this article.

artweb design
Sven Fuchs
Grünberger Str. 65
10245 Berlin, Germany

Fon +49 (30) 47 98 69 96
Fax +49 (30) 47 98 69 97