amatsuda/jb

Unknown keyword problem

zeitnot opened this issue · 3 comments

I am using rails 4.2.7.1. Basically when I set { id: ''} value to .jb template file it says unknown keyword: id. Then I add .to_json at and end of { id: ''} hash expression. But at this stage it produces bad JSON-like string which browsers do not understand.

"{\"id\":\"\"}\n}\n\n"

Due to the fact that there are special characters in this string browsers do not understand whether the string is JSON or not even if the server sends Content-Type: application/json header. Maybe there is a proper way but as I read from the documentation I could not find any way.

JB essentially calls to_json on whatever your template returns as a final value/object, so when you call to_json yourself, you're basically forcing JB to call to_json on an already JSON-ified string, which is why you're getting that 2nd error. I don't know if you have anything (aside from the basic hash you've shown us in your template), but I don't see why id as a key in a hash would cause JB or Ruby to interpret it as a variable rather than a key name. Have you tried using id as a string instead?

'''
{ "id": "" }
'''

@binarydev I have tried the syntax like following:

{ "id": "" }

It says unknown keyword: id. But when I try

{ "id" => "" }

it says no implicit conversion of Hash into String

Update: Problem solved! I had to add render layout: false to my Rails controller action.


Original question:

Did you ever figure this out? I'm running into the exact same problem.

Here's the surrounding code Rails 5.1 shows along with the error message:

      [302, { "Location" => @target }, []]
    else
      @app.call(env) # <-- here's where it goes wrong
    end
  end