简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM