I’ve been getting back into programming full-time, instead of managing developers and fitting my own coding around that time as I could (read 18+ hour work days), by building a family project in Ruby on Rails. I’ve managed, maintained, and debugged a number of RoR projects, but this will be my first from the ground up full build. Now, of course, this is a perfect opportunity to go all out, use the bleeding Edge, and try and do everything right, which suits my perfectionist side just fine.
Part way through the first phases of the project, I come across Markaby and decide to switch away from ERb to that. It’s a way of doing your templating for Rails in pure ruby, which is nice for me, as my ruby needs more practice than my HTML, and it saves me from the verbosity of HTML.
Of course, a little later I come across another way to get a litle further out of the safe mainstream. While working on my functional tests (yes, not only am I learning Rails, I am learning TDD at the same time) of the verify :method => (to prevent GET requests from accessing destructive methods), I take the next step into territory that Rails Edge has been moving in and push further into RESTful stuff a little further. At the most basic this means using HTTP DELETE to destroy an object, instead of a form post. Of course, since browser don’t support DELETE, Rails has a reasonably sightly hack to let you have your back-end code be clean and handle a true DELETE and a slightly hacked form delete in the same way.
Well then I start going further and trying to make the next model I am working on fully REST accessible. This leads me to some of the newer stuff in Rails for that, the merging of simply resful plugin functionality into Rails. Well that and the kick-ass v2 Agile Development with Rails book lead me into using named routes in my form_for of my templates.
form_for :modelname, url => modelname_path…
Well this is where things get a little jinky. Markaby does all sorts of magic and assumes that most helpers called should be directly output to the HTML stream. This is great most of the time, but sometimes you don’t want this, like when said “helper” is a named route and outputting directly into the HTML at that point will not work with your form. Of course, they address this issue , in general. Well it doesn’t quite solve the problem of this specific usage. A commentor on the blog even asks a question about named routes. Unfortunately commenting is closed to move things to the new Markaby Trac, before there is 100% resolution.
So, at the risk of bugging the maintainers, I reopened an old ticket on the issue, and asked about it there. There is some mention of being able to use .to_s and that does work for me, but I hadn’t really seen it documented, and wanted to know if that was the official word on handling named routes. I’ll update here with the response, but for now this does work in Markaby:
form_for :modelname, url => modelname_path(@instance).to_s