Avoid compiling ruby keywords into template locals #26672
This is a change to ActionView::Template. It lets you pass ruby keywords to render:
It prevents confusing syntax errors. Normally you'd see a misleading error on the template's line 1, where there is no error:
Instead it will render successfully. If you use a keyword in your template, you get a syntax error on the line where you use it
The keywords are still available in
I've also made this change available as a gem: https://rubygems.org/gems/actionview_template_safe_locals
cool! I think it's all good now, let me know if I need to amend this with anything.
@matthewd thanks for the super helpful feedback! i'm so happy this PR went from "filter out reserved words" to "only set variables that make sense"
this lets you pass ruby keywords to templates: <%= render 'example', class: "cool" %> <%= render 'example', "spaces are" => "a-ok" %> <%= render 'example', Foo: "bar" %> Previously you'd see confusing syntax errors like this: SyntaxError (.../_example.html.erb:1: syntax error, unexpected '=' Now you can reference invalid identifiers through local_assigns. If you try to use an invalid keyword (e.g. class) in your template, you get a syntax error on the line where you use it.
i wrote a blog post about my experience contributing to rails, and how it really exceeded my expectations. it mentions you folks
In #26672 we blocked use of Ruby keywords as identifiers for view locals but inadvertently broke the use of instance variable names as identifiers. Whilst not explicitly documented this behavior has been around for a long time and there's no need to break it now. Fixes #27480. (cherry picked from commit b5edc55)