Dynamic fields with Rails and JavaScript. How to catch values?
I'm trying create a dynamic form with Rails 3 and JavaScript. I want to add new fields if the user ask开发者_如何转开发s it. But I see that the I can't catch the values from the new fields created with JavaScript. I use the form_tag and text_field_tag to create the form, with the id's hab_0, hab_1, hab_2. But when I generate a new field using a JavaScript function (hab_3, for example) the value can't be catched in the controller. The JS function just add a new field.
So, if I created the fields with the id hab_0 and hab_1 through the form_tag I can catch the values in the controller as params[:hab_0] and params[hab_1]. But if I generate a new field hab_2 with the JS function I get params[:hab_2] = nil.
I guess this is related to the secret_token value that Rails uses to prevent the CSRF attacks... but I'm not sure.
Thanks for reading. I hope you can help me.
you'd better show your javascript code;
Try:
var input = document.createElement('input')
input.name = 'hab_2'
input.id='hab_2'
document.body.appendChild(input)
精彩评论