Amazon.com Widgets
Twitter » Linkedin » Flickr » DjangoPeople » Feed

This site is now an archive. New posts will be written at gregnewman.org. Please follow the new site and update your feeds.

...not so private reflections of greg.newman
RSS feed - Categories & Search

EJS via MinusMor

In a recent project I had to come up with a way to include a partial based on a condition from a dropdown selection. Conditionals in RJS? I don't think so. So thank you Dan Webb for your kick-ass MinusMor plugin. What this does is allow you to embed ruby in javascript as an .ejs extension which gets evaluated before any rjs templates.

So with an observe_field I do:

<%= observe_field 'field_to_evaluate', :url => {:action => 'my_ejs_template_name' } %>

I create an ejs template and insert the following code.

if (document.getElementById('field_to_evaluate').value == "my_true_value") { $('div_container_to_update').update(<%= partial('the_partial_to_show') %>); }

Now the field it is observing is a dropdown menu. Whenever the selection is true, I see my partial.
Again, a big thanks to Dan Webb!

3 Comments

Great addition! Unfortunately it seems to not work with umlauts & other special characters...

Where are you using umlauts and other special characters? Try using the ascii equivalents.

Does it work in Rails 2.0? Because I'm getting error:

=> Booting WEBrick...
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.1/lib/active_support/dependencies.rb:478:in `const_missing': uninitialized constant ActionController::MimeResponds::Responder::DEFAULT_BLOCKS (NameError)
from C:/rails_projects/myproject/vendor/plugins/minus_mor/init.rb:7:in `evaluate_init_rb'