Rails and Merb together

Or putting it in another words: Merb will be merged into Rails to become Rails 3.0. Is it 1st April just way too soon or what? – was my first thought the moment I’ve read it on the Ezra’s blog. But then I’ve read the official announcement and… it kind of turned out to be true. Surprised I was, definitely. I haven’t really jumped on the whole Merb bandwagon. Sure, I was planning to try out Merb in the (rather distant) future. Now it seems I won’t have to. Merb supposedly did lots of things better than Rails (modularity, less dependencies, more lightweight, faster) and those things are to be incorporated into Rails. Will see how this pans out.

Exciting times. Definitely exciting. Nice Xmas gift.

Rails helpers. Rediscovered

Normally, you’d use partials to manage some common functionality in a single file. For example like this:

Somewhere in your view:

<%= render :partial => 'ads/ad', :locals => {:placement => 'frontpage-banner1'} %>

It’s quite concise, but how about making it even less verbose? Helpers to the rescue:

module AdsHelper
  def ad(placement)
    render :partial => 'ads/ad', :locals => {:placement => placement}

And now you can write this in your view:

<%= ad 'frontpage-banner1' %>

Nice! I believe it’s as short as it gets. Sure, if you render this partial only a few times it might not be worth it, but what if you render it 20 or 30 times?

content_tag FTW!

About those one-liners… How about turning this:

<% if logged_in? then %>
<p><%= link_to 'Edit', document_edit_path(@document) %></p>
<% end %>

into this:

<%= content_tag 'p', link_to('Edit', document_edit_path(@document)) if logged_in? %>

I don’t know about you, but to say that I’m impressed would be not enough. Available from Rails 2.0 upwards.

Mongrel_cluster not starting after hard reboot

Does the following error sound familiar?

** !!! PID file log/mongrel.pid already exists.  Mongrel could be running already.  Check your log/mongrel.log for errors.
** !!! Exiting with error.  You must stop mongrel and clear the .pid before I'll attempt a start.

It usually happens when the server crashes. After that you need to ssh into it, remove the mongrel pid files and start the cluster manually. No more.

I assume you have mongrel_cluster setup properly, ie: project’s config file is in /etc/mongrel_cluster and the mongrel_cluster script has been copied from:
to the /etc/init.d directory. You need to edit the /etc/init.d/mongrel_cluster file:

Change this two bits:

  # Create pid directory
  mkdir -p $PID_DIR
  chown $USER:$USER $PID_DIR

  mongrel_cluster_ctl start -c $CONF_DIR


  mongrel_cluster_ctl restart -c $CONF_DIR


  # Create pid directory
  mkdir -p $PID_DIR
  chown $USER:$USER $PID_DIR

  mongrel_cluster_ctl start --clean -c $CONF_DIR


  mongrel_cluster_ctl restart --clean -c $CONF_DIR


Adding the --clean option makes the mongrel_cluster_ctl script first check whether mongrel_rails processes are running and if not, checks for existing pid files and deletes them before proceeding.

You must be using the mongrel_cluster version 1.0.5+ for it to work as advertised (previous versions were buggy). To upgrade do:

gem install mongrel_cluster
gem cleanup mongrel_cluster

Here’s the related mongrel_cluster changeset.

TylkoZyczenia.pl – My third rails app available to the public

TylkoZyczenia.pl – my new Rails website is now live. It’s Polish language only and it will probably stay this way. Basically, it’s a site with collections of wishes for various occasions, like Christmas, New Year, birthdays and so on. At the moment it’s not very different from other, similar websites (and there are lots of them in Poland) apart from the fact that my design, in my humble opinion, is leaner and much more clearer/simple than the others (aka simplicity all the way). I have some ideas to make this site different (read: better) from the others, though. Stay tuned.

It took me about 2 months of intensive, after-hours work. It’s the first version, which is usable to be online (meaning that basic stuff, like registering and adding new wishes work. The TODO list is quite big…). Enjoy!

Tylko życzenia!

Late night capistrano

Excerpt from my current Capistrano cheat sheet:

cap deploy_with_migrations
- will deploy normally and then run 'rake db:migrate' (SCARY!)
- 'cap disable_web' (FIRST!)
- WILL NOT RUN ANY :after_deploy methods (FUCK!)

Now don’t get me wrong, I really love Capistrano (now at 1.4) as it makes my development life a lot easier. It’s just those little bits that bite you in the ass in the most unexpected moment.

Oh, and I’m sure there’s a reasonable explanation for this behaviour.

UPDATE: It has its own ticket now.

Express Kaszubski – my second public Rails app

Express Kaszubski, a local Kaszubian (that’s in Poland) newspaper has just recently decided to stop releasing its printed edition and move entirely to the web. It was my honour to accompany during this move. The online edition took about a month to create. It’s just a basic website at the moment. No comments, no RSS feeds, although the CMS behind the scenes is quite powerful. All in Rails, of course.

Here is the snippet:

Express Kaszubski

And if you haven’t noticed by now, it’s all in Polish.

Kania Lodge – my first public Rails project

To be honest, I’ve been a Rails programmer/enthusiast for quite a while now (something along 1.5 years; I think I’ve first downloaded it in July, 2005) but it’s only now that I can show you my first public project using this awesome framework. So without further ado, here it is, the Kania Lodge:

Kania Lodge

The Rails part on this website is the custom CMS that is backing it all up (based on RedCloth). Most of the hard work was with the design, though (Photoshop, etc.). I know it is a hardly original design as I’m not a full-time designer, yet I’ve done my best for it to look good. I’ve aimed for simplicity, cleanliness and good overall feel/aesthetics. Did I miss or am I right on the spot? :)

Yet another anti-PHP rant

I’m quite surprised to see this great stir in the blogosphere caused by Tim Bray and his essay On PHP. I mean, what’s the deal anyway? We all know that PHP is a lousy programming language. So instead on focusing on the big picture and trying to list as many of its faults as I can remember, I’ll stay on one specific problem, which is really a pain for me.

I’ve been programming in PHP for the past five years (I’ve started using it in the beginning of 2001, during my Computer Science studies). PHP is (was?) both my love and hate. My biggest complain about it, and probably about all other programming languages used for web development, is that there is no decent way of producing HTML code from your application. No matter how hard you try, you’ll always end up having spaghetti either in your HTML or in PHP code (or even worse, in both places). Sure, separating business from presentation logic is a common practice which helps the cause (apart from the fact that most PHP folks mix HTML with PHP whenever and wherever they can). Problem is, that it’s not good enough.

It would be all much simpler if you didn’t need to put any logic into your presentation code. The moment you introduce loops or conditionals – bam – you either get unreadable code or spaghetti HTML with way too much whitespace (or without any whitespace at all) and improper indentation. Not to mention that I am probably not the only one sick of typing all those nasty angle brackets all the time. But what about all that specialized templating engines? Frankly, they are not any better. Besides the fact that some of them require a certain overhead (like defining the variables you wish to use in your templates – Smarty, anyone?), they all still carry the same burden – they either produce spaghetti HTML or have spaghetti templates.

Actually, Rails, being my current weapon of choice, is no better in this area. My .rhtml templates are a complete mess. I strive to have my HTML as pretty as it can be, because (maybe strangely) I find it easier to debug if I have a nice HTML output to deal with, instead of a nice .rhtml template. Too bad that I feel utter disgust when I look at those templates. And just as Smarty is no better for PHP, Liquid and similar projects are no better for Rails. However, there is still hope, with projects like Markaby (which, unfortunately, has its own issues, check the comments).

Just a small digression, to sum all this up. Rails has solved one other, big inconvenience, namely writing your own SQL for each and every interaction with the database. Thanks to ORM provided by ActiveRecord, you don’t have to write SQL any more (at least in most of the cases; the 80/20 rule). This is one of the basic reasons that Rails is so much better than PHP. Markaby tries to do the same to HTML, what ActiveRecord did to SQL. No more those nasty < /> characters! We’ll see how (and where) it goes.