Economy of Effort

Twitter LinkedIn GitHub Mail RSS

Careful What You Name Your Rails Partials

tl;dr: Hyphens bad.

I encountered a strange bug in Rails 3.2.13 this week. I kept getting an error that traced back to the very start of a partial:

_example-partial-logged-in.html.haml:1: syntax error, unexpected keyword_in, expecting keyword_end

I tracked it down to this issue, which pointed out that the error was related to the hyphenated name ending with a Ruby reserved word.

By convention, Rails uses underscores for word separation in file names. Hyphens are not completely disallowed (the above works if I rename the file to end with a non-reserve word), but can lead to issues.

The issue linked above contains a pull request for a better error message, which was merged into Rails 4. But for those still on Rails 3 and earlier, if you see this vague error message, now you know why.

Comments