[Rails] A "Rails" way to design a list-like page with CRUD capabilities?

Jarkko Laine jarkko at jlaine.net
Sat Jan 22 08:09:48 GMT 2005


On 22.1.2005, at 01:06, Carl Youngblood wrote:

> Duane Johnson wrote:
>>
> I think it would be even better to leave the list as plain text but 
> add an edit link next to each person.  When clicked on, the edit link 
> would change the HTML dynamically so that what previously appeared as 
> plain text now appears as HTML inputs, and when the save button is 
> clicked, the editing is saved in the database using XmlHttpRequest and 
> the HTML inputs change back to plain text without having to refresh 
> the page.  This would be the way a next-generation Gmail-like web app 
> would do things.
>

I just did a poor man's version of this in my current rails project. It 
doesn't switch between view and edit state of a row, but it displays an 
edit row below the view row when "edit" link is clicked. It should be 
pretty straight-forward to extend the script to do what you're talking 
about, even the XmlHttpRequest part.

Here's the relevant code (javascript stolen from zeldman's book):

  <script type="text/javascript">
   function toggle( targetId ) {
     if (document.getElementById) {
       target = document.getElementById( targetId );
       if (target.style.display == "none") {
         target.style.display = "";
       } else {
         target.style.display = "none";
       }
     }
   }

   </script>

...

   <% for product in @model.products %>
   <tr>
     <td headers="code"><%= product.code %></td>
     <td headers="sole"><%= product.sole.name_en %></td>
     <td headers="upper"><%= product.upper.name_en %></td>
     <td headers="lining"><%= product.lining.name_en %></td>
     <td headers="price"><%= product.base_price %> &euro;</td>
     <td><a href="#" onclick="toggle('edit_<%= product.id %>');return 
false;" title="Show and hide edit form for this product.">Edit</a></td>
     <td><%= link_to "Delete", :action => "del_product", :id => 
product.id %></td>
     <td><%= link_to "Edit sizes", :action => "product", :id => 
product.id %></td>
   </tr>

   <tr class="edit_row"  style="display: none;" id="edit_<%= product.id 
%>">
     <%= start_form_tag :action => "edit_product" %>
     <td><%= hidden_field "product", "id", "value" => product.id %></td>
     <td><%= collection_select("product", "sole_id", @soles, "id", 
"name_en") %></td>
     <td><%= collection_select("product", "upper_id", @uppers, "id", 
"name_en") %></td>
     <td><%= collection_select("product", "lining_id", @linings, "id", 
"name_en") %></td>
     <td><%= text_field("product", "base_price", "size" => 5, 
"maxlength" => 5, "value" => product.base_price) %> &euro;</td>
     <td><input type="submit" value="Update" /></td>
     <%= end_form_tag() %>
   </tr>
   <% end %>


> Carl
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2363 bytes
Desc: not available
Url : http://one.textdrive.com/pipermail/rails/attachments/20050122/f2db9ff5/smime.bin


More information about the Rails mailing list