Rails url_helpers mixup when using rails engines

1 minute read

I just tried to integrate the blogit rails engine directly inside www.mes-courses.fr. Up till now, I was using a separated radiant app to deal with the blog part. It was working fine, but I had a few problems:

  • I had to use special (javascript heavy) google analytics code through all cross app links, and that sometimes failed (somewhere in the combinaison of heroku, rails, and analytics)
  • It was difficult to share the theme between the apps : on one side, templates, partials and css, on the other, rows in a database
  • It was difficult to share session state between the two apps (login for example)

As my blog is very basic, and as I am the only one editing the styles, I decided to embed a blog engine inside my app. I selected and tried a few engines : Monologue, Blogit, RefineryCMS and to roll my own. Refinery looks way too complex to integrate regarding my basic needs. Monologue is not design to integrate with devise users (which I already use) and I would prefer to contribute to an existing gem rather than to write the nth rails blog engine …

After a few experiments, I settled on blogit. Unfortunately, I had really strange issues with links within blogit pages : all links were prefixed with /blog and blogit links where like /blog/blogit?controller=posts&action=new. Hacking around in the rails console showed me that doing consecultively :

include Blogit::Engine.routes.url_helpers
include Rails.application.routes.url_helpers

It resulted in the same behavior. After a long debugging session, I eventually discovered that I had a helper in my application which explicitly required Rails.application.routes.url_helpers. This was causing the conflict. I changed my code to avoid this inclusion, and that fixed the links …

… nearly. Now rendering the application template for a blog page failed with a “message not understood” for the main app’s xxx_path methods. Hopefully, I had found a fix for this error earlier on the web. Now everything was working like a charm.

To conclude, I was convinced by blogit, and I am going to use it to migrate my blog. I already have a few ideas for improvements I will have to do (disqus comments and archive helper for example).

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Leave a comment