Ajax form submission and updating multiple divs


#1

I’m not sure if this is even possible, but here is my current method:

class Item < ActiveRecord::Base

def self.generate(name)
Item.create(:name => name) || nil
end
end

class ItemController < ActionController::Base

layout “application”

def index
@items = Item.find(:all)
end

def create
if @request.method ==:post
name = @params[:name] || ‘’
if name != ‘’
if Item.generate(name)
@message = “success”
else
@message = “failed”
end
else
@message = “please enter a value”
end
@items = Item.find(:all)
render(:partial => “itemlist”)
end
end
end

index.rhtml

<%= form_remote_tag(:update => “itemlist”, :url => { :action => :create
})
%>
<label for “name”>Enter Item Name:
<%= text_field_tag :name %>
<%= submit_tag “Save Item” %>
<%= end_form_tag %>

<%= render(:partial => "itemlist") %>

_itemlist.thtml

<%= @message %>

<% @items.each do |i| %> <% end %>
ID Barcode created
<%= i.id %> <%= i.barcode %> <%= i.created_at %>

now, what i would like to do is separate out the message and table into
2
divs. after submitting, if there is a failure, only update the message
div
with success/failure message and leave the list div alone. if there was
success, update both divs with the necessary info. currently, i update
the
div for every submission, incurring a hit to the item table to generate
a
list whether the submission was successful or not. is there a way to
accomplish what i would like to do?

Chris