Topic: Create multiple seperate AJAX forms on one page?

I didn't seem to get much of a response to my last post, so I figured it might help if I rephrased the question. I want to create a page that will show an edit form for job in Job.find(:all). I also want each individual form on the page to have its own button to update an individual job. This should work through ajax so that there is no page refresh. Currently, I am able to get a page with all of the forms, but I can't get it to work without a refresh and updating every single job on the page. Any ideas?

Re: Create multiple seperate AJAX forms on one page?

Are you using form_remote_tag on the forms?

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Create multiple seperate AJAX forms on one page?

I have the following (I have removed most of the formatting for clarity)

<% for @job in @jobs %>
<% form_remote_for "job[]",:url => { :action => "ajax_job_update"} do |j|  %>

<%= j.text_field :title, :size => 15 %>
# a whole bunch more fields here
<%= submit_tag("Save this Job") %>
<% end %>


Controller code
  def ajax_job_update
    @job = Job.find(params[:id])
    if @job.update_attributes(params[:job])
      flash[:notice] = 'Job was successfully updated.'
     
    end
       
  end

When I push the submit button, nothing happens (not even in the database) and the development log says something about "
Rendering C:/INSTAN~1.6/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.2/lib/action_controller/templates/rescues/layout.rhtml (500 Internal Error)"

Re: Create multiple seperate AJAX forms on one page?

Hmmm... I seem to have gotten a little closer.

I now have this in the view

<% for @job in @jobs %>
  <% form_remote_for "job",:url => { :action => "ajax_job_update",:id=>@job}  do |j|  %>
 
    <%= text_field :job, :title, :size => 15 %>
   
  <%= submit_tag("Save this Job") %>
  <% end %>
<% end %>

This works, but only if I replace the @job = Job.find(params[:id]) line in the controller with @job = Job.find(#) where # is the actual id of any one job. Unfortunately, this means I can only edit one job on the page at any one time, but it does work. The problem seems to be that params[:id] never gets the value of the current job when the ajax call executes. The dev Log shows this :

Processing AdminController#ajax_job_update (for 127.0.0.1 at 2007-08-05 20:41:01) [POST]
  Session ID: c6f3d9c1e1cf516bb217cda05a3896a8
  Parameters: {"commit"=>"Save this Job", "action"=>"ajax_job_update", "controller"=>"admin", "job"=>{"title"=>"New Title"}}

I would think that there should be something like "id"=>"12" or something like that.

Re: Create multiple seperate AJAX forms on one page?

What's in the HTML for the form?

I snipped this example:

form_remote_tag :html => { :action => url_for(:action => 'action', :id => obj), :method => :post }

From:
http://blog.invisible.ch/files/rails-re … #ajaxforms

Last edited by tobyhede (2007-08-06 00:06:02)

Toby Hede
===================================================
FiniteStateMachine - Software Development for Social Networks
===================================================

Re: Create multiple seperate AJAX forms on one page?

 ## shouldn't this
<% form_remote_for "job",:url => { :action => "ajax_job_update",:id=>@job}  do |j|  %>
## be this?
<% form_remote_for "job",:url => { :action => "ajax_job_update",:id=>@job.id}  do |j|  %>