Hi
Im new to the ruby/rails/ajax trilogy, nevertheless.
What i want to do?
Basic CRUD operations using AJAX
Approach
1) I created a ItemController using the ruby script/generate scaffold
item item command (so that its easier for me to add/remove date while
trying the ajax kindoff operations)
Table items contains 2 fields - id and name(varchar)
2) defined a new method in ItemController named ajax_del, ajax_edit,
ajax_new
def ajax_del
tmpItem=Item.find(params[:id])
tmp=tmpItem.name
tmpItem.destroy
render :inline => "<%=
update_element_function(''#{tmp}'', :action
=> :remove)%>"
end
lets only deal withthis one method for now
3) changed the links in the list.rhtml to perform ajax operations
<h1>Listing items</h1>
<table>
<tr>
<%= javascript_include_tag "prototype" %>
<% for column in Item.content_columns %>
<th ><%= column.human_name %></th>
<% end %>
</tr>
<% for item in @items %>
<tr>
<div id="<%=item.name%>">
<% for column in Item.content_columns %>
<td><%=h item.send(column.name) %></td>
<% end %>
<td><%= link_to ''Show'', :action =>
''show'', :id => item %></td>
<td><%= link_to_remote("Edit", :update =>
"#{item.name}",:url=>{:action => :ajax_edit, :id => item})
%></td>
<td><%= link_to_remote( "destroy",:update =>
"#{item.name}",:url =>{
:action => :ajax_del, :id=>item}, :comlpete=> :evaluate_remote_response
) %></td>
</div>
</tr>
<% end %>
</table>
<%= link_to ''Previous page'', { :page =>
@item_pages.current.previous }
if @item_pages.current.previous %>
<%= link_to ''Next page'', { :page =>
@item_pages.current.next } if
@item_pages.current.next %>
<br />
<div id="newItem">
<%= link_to_remote("New
item",:update=>"newItem",:url=>{:action =>
:ajax_new}) %>
</div>
4) hit http://localhost::3000/item and click on the destroy link.
Results
when i click on the destroy link, teh particular item is deleted from
teh databse; but istead of removing the dom element it appends teh text
*Element.remove(elementId);* on top instead of removing the whole
element from the view(browser view)
Please help
Thanks in advance
Shodhan Sheth