Form values not included in submit when ajax generated

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é

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?

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é

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é