maha
maha

Reputation: 607

rails how do I convert a variable into a symbol for use in a JSON string?

Trying to build a JSON string for use with a service and want to pass some of the JSON keys as variables.

I've tried:

def blah( field, value ) 
...
body = { request: { field.to_sym value } }

and

body = { request: { field.to_sym: value } }

but get errors in the console. What's the proper syntax here?

Thanks.

Upvotes: 1

Views: 404

Answers (1)

mu is too short
mu is too short

Reputation: 434585

I'm guessing that you're looking for the hashrocket syntax:

body = { request: { field.to_sym => value } }
# -------------------------------^^

The JavaScript-style k: v notation can only be used with a limited set of literal symbols.

Upvotes: 1

Related Questions