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
-
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…
-
<html>
-
<head>
-
<title> Project </title>
-
<%= include_required_js %>
-
</head>
-
<body>
-
<%= catch_content :for_layout %>
-
</body>
-
</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.
-
<%- require_js "prototype" %>
-
-
<div id="content">
-
blablabla
-
</div>
And the result:
-
<html>
-
<head>
-
<title> Project </title>
-
<script src="/javascripts/prototype.js" type="text/javascript"></script>
-
</head>
-
<body>
-
<%= catch_content :for_layout %>
-
</body>
-
</html>
It was 4 hour in the morning, I downloaded the newest version from git, and the solution was dependency merb-assets. But works ![]()
Merb - First steps
Posted by Nucc
I’ve read a lot of Merb, so I decided to try it. First it seems to be very poor framework, and it’s true. It’s only a skeleton unlike rails. First I tried
script/generate model User name:string email:string
and I got a pure User class without Datamapper, ActiveRecord, or other ORM. After googling I found the solution, it need to set use_orm :datamapper in dependencies.rb. For datamapper install use this:
sudo gem install datamapper sudo gem install merb_datamapper sudo gem install do_mysql
It’s okay, we generate again our model file.
script/generate model User name:string email:string
The result:
-
class User < DataMapper::Base
-
property :name, :string
-
property :email, :string
-
end
Okay, it’s nice, but how can we migrate with the database? We have a config/database.sample.yml file like in rails, edit this. It’s very nice, in Rails we have to set username, pass, database for all environment, here we can use inheritance.
:development: &defaults :adapter: mysql :database: merb_test :username: root :password: :host: localhost :test: <<: *defaults #:database: sample_test: production: <<: *defaults #:database: sample_production
Okay, it’s nice, try to migrate. … I’ve need some time, to solve this very simple problem. In rails, we have rake db:migrate, but in merb it’s not, or not ready yet. Okay, no problem, I found this solution:
In config/merb_init.rb insert this:
-
DataMapper::Persistence.auto_migrate!
It will migrating our models with database before merb server start. Let’s look an example. If we want to add a column to our User table, we only need to set another property tag, like:
-
class User < DataMapper::Base
-
property :name, :string
-
property :email, :string
-
property :message, :string
-
end
After a restart Users table looks:
desc users; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | YES | | NULL | | | email | varchar(50) | YES | | NULL | | | message | varchar(50) | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+
Nice. But! I’ve some problems. How can we manage, when new columns have an initialize value? How can we rename row? It’s very good for smaller projects, but what we can do, if we have an already exists database with a lot of data? It’s not trivial for me?
I shouldn’t remove migrating, but change it. I forget the numbers before the migration name, and use creation date for order them. Every migration file should has a creation date, and migration script use it for order them. It’s only an idea.
Another problem in Merb, controllers don’t get Controller suffix in class name. It seems to comfortable, but what happens, if we have a User model, and we want to use an User controller to manage our model. It’s problem. Need to use different namespaces for models and controllers, or use suffix or prefix in controllers like rails.