# Form values not included in submit when ajax generated

**URL:** <https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155>\
**Category:** Rails\
**Created:** [November 19, 2005, 11:59am UTC](https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155 "2005-11-19T11:59:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@AndrSSSS](https://www.ruby-forum.com/u/AndrSSSS)\
**Post date:** [November 19, 2005, 11:59am UTC](https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155/1 "2005-11-19T11:59:26Z")

</div>

Hi.

I have a form with two select lists, named ‘project’ and ‘activity’. The  
content of the ‘activity’ list is changed when the selected item in  
project changes, using ‘observe\_field’ where project is observed, and a  
div enclosing ‘activity’ is updated. This works as expected. The problem  
is that when submitting the form containing the two select list after  
they have changed, the value of the selected ‘activity’ item is not  
included in the submitted values. If no change in the selects has  
occurred since the page was originally loaded, the value for ‘activity’  
is included, because the activity list is not generated by the  
‘observe\_field’ the first time.

So I guess the reason that the value of the ‘activity’ select is missing  
is because it is generated by a Ajax call after the form is originally  
loaded, but how can I have the value of the selected ‘activity’ to be  
included in the submitted values? Any ideas?

AndrÃ©

---

<div class="post-metadata">

**Author:** ![AndrSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@AndrSSSS](https://www.ruby-forum.com/u/AndrSSSS)\
**Post date:** [November 19, 2005, 3:33pm UTC](https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155/2 "2005-11-19T15:33:53Z")

</div>

André wrote:

> Hi.
> 
> I have a form with two select lists, named ‘project’ and ‘activity’.

Can you send us some code? How do you use the observe\_field helper, and  
what does the code you send back to the call looks like?

---

<div class="post-metadata">

**Author:** ![AndrSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@AndrSSSS](https://www.ruby-forum.com/u/AndrSSSS)\
**Post date:** [November 19, 2005, 11:01pm UTC](https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155/3 "2005-11-19T23:01:15Z")

</div>

Simon.Santoro wrote:

> Can you send us some code? How do you use the observe\_field helper, and  
> what does the code you send back to the call looks like?

I am traversing an array of objects, lets call them items.  
For each item I creates a table row, and puts in the projects and  
activities selection lists there. As you can see I also creates a div  
with a different name for each of activity selection list for each row.  
…  
…

\<%= start\_form\_tag :action =\> ‘save\_item’ %\>  
\<%  
idx = 0  
for @item in @items  
-%\>

 \<%= select("item[]" , "project\_id", @projects.collect {|p| [p.name, p.id] }, {}, {:id =\> 'item\_project\_id\_' + idx.to\_s}) -%\> 
 \<%= select("item[]", "activity\_id", @projects[0].activities.collect {|pa| [pa.name, pa.id] }) %\> 
 \>

\<%= observe\_field ‘item\_project\_id\_’ + idx.to\_s, :frequency =\> 0.2,  
:update =\> ‘activities\_’ + idx.to\_s, :url =\>  
{ :action=\> :activity\_for\_project },  
:with =\> “‘project\_id=’ + escape(value) + ‘&’+ ‘index=#{idx}’ +  
‘&’+ ‘item\_id=#{@item.id}’”  
-%\>  
\<%  
idx += 1  
end  
%\>  
…  
…

The partial contains:

 \<%= select("item[]", "activity\_id", @project\_activities.collect {|pa| [pa.name, pa.id] }) %\>

and the activity\_for\_project method in the controller:

def activity\_for\_project  
@item = Item.find(params[:item\_id])  
if not @item  
@item = Item.new  
end  
@project\_activities = Activity.find(:all, :conditions =\> “project\_id  
= #{params[:project\_id]}”)  
render(:partial =\>“activity\_for\_project”, :object =\>  
@project\_activities, :locals =\> {:index =\> params[:index]})  
end

AndrÃ©

---

<div class="post-metadata">

**Author:** ![AndrSSSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e99b99/32.png) [@AndrSSSS](https://www.ruby-forum.com/u/AndrSSSS)\
**Post date:** [November 22, 2005, 10:34pm UTC](https://www.ruby-forum.com/t/form-values-not-included-in-submit-when-ajax-generated/100155/4 "2005-11-22T22:34:57Z")

</div>

Hi again.

After going through the rhtml once again I saw what caused my problem.  
It is as simple as that the start\_form\_tag started inside the table,  
with the corresponding end\_form\_tag after the tag. Moving the  
start\_form\_tag before the

 solved the problem.

Feeling shameful? You bet!

AndrÃ©
