Catching relative url issues in rails early with --prefix

When developing in Rails, we use script/server to run the app, which makes the application available at http://localhost:someport. Later, in production it's usually somedomain.com/. In both cases, the application is mounted at the root of the domain, which means links beginning with / (like say <a href='/users'>Users</a>) which were inadvertently introduced will never break. Then, when someone then deploys your application to a non-root url like mydomain.com/myapp all hell breaks loose and you have a bunch of broken links.

As a preventive measure, it's wiser to both develop and run automated functional test suites (like a Selenium suite) with your application running on a non-root url. That way relative url bugs are caught early enough that nothing significant is affected.

Some ruby servers do indeed support this kind of mounting - but not all, I presume, so script/server does not support it. However, the popular mongrel server does allow this through the --prefix option. We've modified script/server to a more narrow script/mongrel which accepts the --prefix option and always uses mongrel. script/mongrel otherwise behaves like script/server in every respect.

Try it out - you can get it off GitHub from the script-mongrel project. If you're on Windows (or otherwise don't have git), don't worry - it's a single file plus the README. You can simply copy them off the repo browser.

2 comments:

Anonymous said...

This issue is finally resolved in Rails 2.3.2; the option there is --path instead of --prefix, e.g.:
ruby script/server -p 3000 --path=/myapp

Gladwright said...

Newbie here. What's the solution to the relative bug? I've been wanting to get completely relative paths that don't start with a slash out of the link_to and image_tag helpers but can't figure out how. Thanks.