gazay / gon

Your Rails variables in your JS
MIT License
3.05k stars 184 forks source link

Parse error when using jbuilder partials without an options hash #224

Open RavenXce opened 7 years ago

RavenXce commented 7 years ago

Steps to reproduce:

  1. Call a partial with no options hash:
# new.json.jbuilder
json.foo do
  json.partial! 'foo/partial'
end
  1. Render the jbuilder template
# foo controller
def new
  gon.jbuilder
end

Expected result:

The template is assigned to the JS gon.foo variable successfully.

Actual result:

Exception thrown: NoMethodError: undefined method '[]' for nil:NilClass Stacktrace:

21:29:06 web.1   |   gon (6.1.0) lib/gon/jbuilder/parser.rb:78:in `parse_partial'
21:29:06 web.1   |   gon (6.1.0) lib/gon/jbuilder/parser.rb:119:in `block in find_partials'
21:29:06 web.1   |   gon (6.1.0) lib/gon/jbuilder/parser.rb:117:in `find_partials'
21:29:06 web.1   |   gon (6.1.0) lib/gon/jbuilder/parser.rb:22:in `parse!'
21:29:06 web.1   |   gon (6.1.0) lib/gon/jbuilder.rb:18:in `handler'
21:29:06 web.1   |   gon (6.1.0) lib/gon.rb:96:in `jbuilder'
...

Notes:

It seems that line 78 of the parser tries to access the MatchData array even though String#match will return nil when there are no matches. The options_hash will therefore never be empty in a valid jbuilder template, meaning that the check is wrongly done.