Forum: Ruby on Rails Form values not included in submit when ajax generated.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C2ad4ca0f1ad88fdf7c07d017cc9fc7d?d=identicon&s=25 André (Guest)
on 2005-11-19 11:59
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é
B6e90ad1e69d563f1f13cccffe137262?d=identicon&s=25 Simon.Santoro (Guest)
on 2005-11-19 15:33
(Received via mailing list)
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?
C2ad4ca0f1ad88fdf7c07d017cc9fc7d?d=identicon&s=25 André m Bonkowski (bonski)
on 2005-11-19 23:01
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
-%>
<tr>
	<td><%= select("item[]" , "project_id", @projects.collect {|p| [
p.name, p.id ] }, {}, {:id => 'item_project_id_' + idx.to_s}) -%></td>
   <td>
	<div id='activities_<%= idx.to_s-%>'>
      	<%= select("item[]", "activity_id",
@projects[0].activities.collect {|pa| [ pa.name, pa.id ] }) %>
    	</div>
   </td>
></td>
</tr>

<%= 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:

<div id='activities_<%= index.to_s-%>'>
  <%= select("item[]", "activity_id", @project_activities.collect {|pa|
[ pa.name, pa.id ] }) %>
</div>


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é
C2ad4ca0f1ad88fdf7c07d017cc9fc7d?d=identicon&s=25 André m Bonkowski (bonski)
on 2005-11-22 22:34
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 </table> tag. Moving the
start_form_tag before the <table> solved the problem.

Feeling shameful? You bet!


André
This topic is locked and can not be replied to.