Controller won't accept a variable number of inputs...

I’m using Prototype to allow the user to add form fields dynamically. However, each field is the same model over and over again (think of it as allowing the user to create a lot of Blog posts at once). So I’m unable to use accepts_nested_attributes_for, since each blog is its own row in the database. No biggie though because I’m able to give each blog its own ID using javascript and the getTime function.

But, I can’t figure out how to get my Blog controller to accept these new dynamically generated form fields. I’m using each_with_index to properly create unique ID’s for each blog (I know there are newer ways, but can’t seem to get those to work, and anyway, I don’t think that’s the problem).

Currently, I have:   def new     @blogs = {}   end

  def create     @blogs = params[:blogs].values.collect { |blog| }     if @blogs.all?(&:valid?)       @blogs.each(&:save!)       redirect_to user_profile_path(current_user.username)     else       render :action => 'new'     end   end

This produces one blog entry form by default, and when I submit it, it gets added perfectly. (If I increase the default to 3 or something, all those get submitted as well). However, the ones I add using my Javascript do not. The javacript ID’s are formatted correctly and everything is correct when I inspect them in Firebug, but these new fields are not submitted with the form.

What am I doing wrong? Is it because I’m limiting the array to 1? but allowing the user to add more? Is there a way to allow Array to accept any and all, but only display 1 by default?

Any ideas on this are seriously appreciated.

new Array(1) just means create an array initially containing 1 object (nil). There aren't ever any restrictions on how many items you can stick in an array (besides if the problem is with javascript created form elements then problem is happening long after the array stopped being relevant. What does the javascript to add the form fields look like? How have you confirmed that the extra fields have not been submitted ?


Thank you Frederick for clearing that up for me. So I guess the problem is in my Views?

Here’s the Javascript to add more fields: <%= add_object_link 'Add Another Blog', 'tasks', :partial => 'blogs' %>

‘tasks’ is only the div id And here’s the helper

  def add_object_link(name, where, render_options)     html = render(render_options)     link_to_function name, %{       Element.insert('#{where}', #{html.to_json}.replace(/ index_to_replace_with_js/g, new Date().getTime()));     }   end

and finally the partial <% @blogs.each do |blog| %>   <% fields_for "blogs", blog, :index => "index_to_replace_with_js" do | f> %>     <%= f.error_messages %>       <%= render :partial => "form", :locals => { :f => f } %> <% end %> <% end %> The actual form fields in the partial named “form” are just your typical f.label and f.text_field entry stuff.

“How have you confirmed that the extra fields have not been submitted ?”

By submitting the form manually and looking at the logs. Only the first one gets added, or however many I set in the Array through the controller. None of the Javascript added fields get added, although their html is formatted correctly. It’s like it doesn’t even see them.

So, I removed the fields in the view set by the Array, so there were no fields and only the “Add Another Blog” link was present. I then added 1 form via the javascript and clicked on Submit. This is the error I get: undefined method `values' for nil:NilClass

C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/ whiny_nil.rb:52:in `method_missing' C:/Rails/AppName/app/controllers/blogs_controller.rb:9:in `create'

So you’re right, the problem isn’t with my controller, but my form. I think the Javascript is correct, but I’m doing something funky with form. This is the “new” view that sets the number of default fields according to the number in the Array. <% form_tag :action => 'create' do %>   <% @blogs.each_with_index do |blog, index| %>     <% fields_for "blogs[#{index}]", blog do |f| %>         <%= f.error_messages %>       <%= render :partial => "form", :locals => { :f => f } %>

Maybe the problem is with the form_tag :action => 'create' do line? I’m really not sure, but any more insight into this is greatly thanked.

Oh my god, I found out what I was doing wrong: I had the <div id="tasks"> BEFORE the form_tag in the views, it should have gone afterward.

So it now looks like this: <% form_tag :action => 'create' do %> <div id="tasks">   <% @blogs.each_with_index do |blog, index| %>

Doesn't always come down to something so simple?

Anyway, thanks again Frederick, much appreciated.