3

I've got the following jQuery variable:

var numbers = ["1", "2", "3"]

I want to submit a form with these numbers so that my params variable looks like this in the controller:

{ 
  "evaluation" =>
  {
     "batch_subject_ids" => ["1", "2", "3"]
  }
}

I am creating this array on the fly when I click a button

jQuery('<input>').attr({
  type: 'hidden',
  id: 'batch_subject_ids',
  name: 'evaluation[batch_subject_ids][]',
  value: batch_subject_ids
}).appendTo('form');

In the controller action, I receive the following params:

{ 
  "evaluation" =>
  {
     "batch_subject_ids" => ["1,2,3"]
  }
}

I can parse and replace the array, but I was wondering if I could send it from the client with the format I want without having to do that. How can I accomplish this? Thank you

4

1 回答 1

1

阅读http://guides.rubyonrails.org/action_controller_overview.html#hash-and-array-parameters

您需要发送单独的 batch_subject_ids[] = 1、batch_subject_ids[] = 2 等值,以便将其转换为数组。

于 2013-11-08T17:15:25.793 回答