I want to create person objects in my rails app using a jquery ajax call. It's fine for one call to one object creation. What I would like is to pass an array of names and to create multiple objects in one trip.
Any ideas?
Thanks
I want to create person objects in my rails app using a jquery ajax call. It's fine for one call to one object creation. What I would like is to pass an array of names and to create multiple objects in one trip.
Any ideas?
Thanks
There's two stages to this: the first is non-ajax related and is basically "how should i do a controller action to create/update multiple objects at once". This has been a common question since the invention of rails, and people have dealt with it in a variety of ways (google "rails multiple update"). Once you have this controller action you could call it from a normal form or from an ajax request, it doesn't really mattter.
So, we just need a controller action that gets some names and makes users. I would make it flexible, and have it so that the data is held in an array in params[:users], each entry in the array being a hash of parameters (so just like params[:user] in a regular create/update call)
#needs to be added to your routes as a :post action
def multiple_create
@users = []
@failed = []
params[:users].each do |user_hash|
user = User.new(user_hash)
if user.save
@users << user
else
@failed << user
end
end
end
Now, this will expect params to have this form (if you send through names for example):
params: {:users => [{:name => "Alice"}, {:name => "Bob"}]}
Now you just need to tailor the data in your ajax query accordingly. I don't know where the data is coming from as you give no details of the page in your question, but it could be something like this (let's assume there's a bunch of inputs for the names that all have the class "newUserName"):
//build the array
var users = new Array();
$(".newUserName").each(function(){
var user = {"name": $(this).val()};
users.push(user);
});
//make the request
jQuery.ajax({
async:true,
type: "POST",
url: ("/users/multiple_create/"),
data: {"users": users},
dataType: "script"
});
Although, i just tested this and while the javascript object in users looks like this:
[{name:"alice "}, {name:"bob "}, {name:"charlie"}]
, the params it actually sends through look like this:
{"users"=>{"0"=>{"name"=>"alice "}, "1"=>{"name"=>"bob "}, "2"=>{"name"=>"charlie"}}}
So, it looks like jquery doesn't want to send the array as a parameter and instead converts it into a hash, with unique keys in the form of 0,1,2 etc to distinguish between the different hashes. This is better way to build up the params anyway to be honest as it makes it less easy to get them mixed up together in a regular form. We can cope with this by amending the controller action as follows:
if params[:users].is_a?(Hash)
@user_hashes = params[:users].values
else
@user_hashes = params[:users]
end
@user_hashes.each do |user_hash|
...
I'm not familar with ruby-on-rails but how about this way?
Looping through your array and create key-value pairs. The key is let's say 'name' -extended by the index- and the value is the persons name. Make sure you send the data with post method, so you get enough space to send a big number of name. On serverside, read all parameters and create your objects, by using a loop.