I am using Rails 3.2.14 without issue...
I have recently renamed application.js
to application.js.coffee
and am now getting a JSON error.
JSON::GeneratorError
only generation of JSON objects or arrays allowed
(in /.../app/assets/javascripts/application.js.coffee)
Even when I delete all the contents of application.js.coffee
I still get the error.
When I try to view it directly (http://localhost:3000/assets/application.js
), its the same issue:
throw Error("JSON::GeneratorError: only generation of JSON objects or arrays allowed\n (in /.../app/assets/javascripts/application.js.coffee)")
I have combed my app for any potential issues but everything looks pretty standard.
In CoffeeScript there are two different types of arrows for defining functions: thin arrows -> and fat arrows =>. The JavaScript function keyword was replaced with the thin arrow. The fat arrow serves as the function keyword and also binds the function to the current context.
CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.
I had a similar problem (Rails 4 asset pipeline throws "only generation of JSON objects or arrays allowed"), which had nothing to do with application.js. The multi_json gem got updated from 1.7.8 to 1.7.9, and broke my app. Any change to the cofeescript-files resulted in this "only generation of JSON objects or arrays allowed" error.
I explicitly put the multi_json gem in my Gemfile, fixed to version 1.7.8. In solved the problem for me.
I have the save issue not in a Rails project, but a ruby project using sprockets. I have not finally determined the problem, but it seems to be related to execjs and the Javascript runtime used.
You can try to add
gem 'therubyracer'
to your Gemfile. This installs the v8 engine, which solved the problem for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With