Forum: Ruby on Rails ajax form submission and updating multiple divs

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.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-02-10 17:02
(Received via mailing list)
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:</label>
<%= text_field_tag :name %>
<%= submit_tag "Save Item" %>
<%= end_form_tag %>
<div id="itemlist">
<%= render(:partial => "itemlist") %>
</div>

_itemlist.thtml

<%= @message %>
<table border="1">
<tr>
    <th>ID</th>
    <th>Barcode</th>
    <th>created</th>
</tr>
<% @items.each do |i| %>
<tr>
    <td><%= i.id %></td>
    <td><%= i.barcode %></td>
    <td><%= i.created_at %></td>
</tr>
<% end %>
</table>

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
This topic is locked and can not be replied to.