# Controller won't accept a variable number of inputs

**URL:** <https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527>\
**Category:** Rails\
**Created:** [February 16, 2010, 4:33am UTC](https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527 "2010-02-16T04:33:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [February 16, 2010, 4:33am UTC](https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527/1 "2010-02-16T04:33:24Z")

</div>

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 = Array.new(1) {Blog.new}  
end

def create  
@blogs = params[:blogs].values.collect { |blog|  
current\_user.blogs.build(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.

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [February 16, 2010, 10:07am UTC](https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527/2 "2010-02-16T10:07:34Z")

</div>

On Feb 16, 3:32 am, AlwaysCharging [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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?

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 ?

Fred

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [February 17, 2010, 12:43am UTC](https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527/3 "2010-02-17T00:43:13Z")

</div>

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.

On Feb 16, 4:05 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [February 17, 2010, 12:53am UTC](https://www.ruby-forum.com/t/controller-wont-accept-a-variable-number-of-inputs/183527/4 "2010-02-17T00:53:45Z")

</div>

Oh my god, I found out what I was doing wrong:  
I had the

BEFORE the form\_tag in the views, it should have gone afterward.

So it now looks like this:  
\<% form\_tag :action =\> ‘create’ do %\>

 \<% @blogs.each\_with\_index do |blog, index| %\>

Doesn’t always come down to something so simple?

Anyway, thanks again Frederick, much appreciated.
