Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON error with coffeescript & Rails asset pipeline

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.

like image 737
brewster Avatar asked Aug 23 '13 04:08

brewster


People also ask

What is-> in CoffeeScript?

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.

What language is CoffeeScript?

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.


2 Answers

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.

like image 67
Erik Avatar answered Sep 30 '22 17:09

Erik


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.

like image 27
Konstantin Krauss Avatar answered Sep 30 '22 19:09

Konstantin Krauss