20seven

Devigner blog about development and design

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!