Rails is unable to load a partial that has only numbers (or if numbers lead before alphabetical characters), and I'm not sure if it's an error on my part, or just an expected behavior of render.

I have:

<%= render "pages/announcements/#{params[:date]}" rescue render 'pages/announcements/none' %>

and two partials:
pages/announcements/_none.html.erb
pages/announcements/_2014_09_01.html.erb

The numerical file name is always ignored, and the 'none' partial is loaded.

When I change this to have a leading alphabetical character, all is fixed:

<%= render "pages/announcements/news_#{params[:date]}" rescue render 'pages/announcements/none' %>

with:
pages/announcements/_none.html.erb
pages/announcements/_news_2014_09_01.html.erb

Can someone explain this behavior or my error?

1

There are 1 answers

3
Dave Slutzkin On BEST ANSWER

If you remove the rescue you'll probably get a more helpful error message:

render partial: '909'

Gives:

The partial name (909) is not a valid Ruby identifier; make sure your partial name starts with a lowercase letter or underscore, and is followed by any combination of letters, numbers and underscores.

This seems to be because ActionView internally converts the partial name into a symbol.

(I bumped into this recently. Putting a prefix on the partial name wasn't a huge imposition in my case, hopefully the same is true of yours.)