Another day with merb and merb-assets

Posted by Nucc

I had a long morning with javascripts helpers. Two days ago, I had installed merb-0.9.0, because I have would try to probe what new features get. The main changes is the separation of merb and merb-gen. The scripts directory was removed, and we can generate models, controllers, … with merb-gen.

merb-gen project
cd project
merb-gen model user

It was long time, until I found this. I started to write some models, controllers, and views. And views… This cause my long-long morning. In merb, we can seperate the javascript code and the layout. It’s very important, when we have a lot of views with one common layout. Let’s look, how does it work… First, (it’s very important!!!) we need to set in config/init.rb

  1. dependency "merb-assets"

It contains our javascript helpers. In merb we can set bundles, but this is another story. Next, we create the layout, it will be a very simple layout…

  1. <html>
  2.   <head>
  3.     <title> Project </title>
  4.     <%= include_required_js %>
  5.   </head>
  6.   <body>
  7.     <%= catch_content :for_layout %>
  8.   </body>
  9. </html>

include_required_js is a target, that will insert the

Let us look the Users/create view. In this view, we will use prototype framework.

  1. <%- require_js "prototype" %>
  2.  
  3. <div id="content">
  4.   blablabla
  5. </div>

And the result:

  1. <html>
  2.   <head>
  3.     <title> Project </title>
  4.     <script src="/javascripts/prototype.js" type="text/javascript"></script>
  5.   </head>
  6.   <body>
  7.     <%= catch_content :for_layout %>
  8.   </body>
  9. </html>

It was 4 hour in the morning, I downloaded the newest version from git, and the solution was dependency merb-assets. But works :)