Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In Rails I'm rendering a json array, but I need one of the keys to have a variable name depending on the params. something like this:

render json: {
        key1: values
        params[:type]: more_values,
        labels: some_lables
      }

obviously this doesn't work, but what will?

share|improve this question
    
you miss a comma after key1: values –  inye 4 hours ago

2 Answers 2

Use interpolation :

render json: {
        :key1 => values,
        :"#{params[:type]}" => more_values,
        labels: some_lables
      }
share|improve this answer

Make a hash and render it as JSON.

h = { key1: values, labels: some_labels }
h[params[:type]] = more_values
render h.to_json
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.