Duane Johnson
2005-Jan-19 00:57 UTC
A "Rails" way to design a list-like page with CRUD capabilities?
So I''ve finally figured out how to make simple and elegant Create/Read/Update/Delete events for a single object such as @person. But how do the Rails experts do it for a list of objects? For example: class TestController < ApplicationController def list @people = Person.find_all end end The corresponding list.rhtml view will elegantly display the array of Person objects. But what if each "Person" row in the view is editable? For example, the view I am imagining would generate a text box for the first name, last name and phone number, all the way down the list, and a check box next to each person to delete the corresponding person from the database. Is there an elegant solution to this pattern in Rails? Any sample code from those of you who''ve come across this and solved it before? Thank-you, Duane Johnson (canadaduane)
Brian L.
2005-Jan-19 01:54 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
I''ve often wondered the same thing. Given the way that rails parses CGI, you could make fields named people[2][firstname] and then update Person.find(2) with the data in @params[''people''][2] (assuming you use attr_protected in the appropriate places in your model, of course). This assumes that 2 is the id. Then at the bottom you put your update button. I''m kinda pulling this out of nowhere but I think it will work. For simple ''click to remove'' buttons, I tend to use a link with a get request to immediately remove the item. Brian On Tue, 18 Jan 2005 17:57:43 -0700, Duane Johnson <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So I''ve finally figured out how to make simple and elegant > Create/Read/Update/Delete events for a single object such as @person. > But how do the Rails experts do it for a list of objects? > > For example: > > class TestController < ApplicationController > def list > @people = Person.find_all > end > end > > The corresponding list.rhtml view will elegantly display the array of > Person objects. But what if each "Person" row in the view is > editable? For example, the view I am imagining would generate a text > box for the first name, last name and phone number, all the way down > the list, and a check box next to each person to delete the > corresponding person from the database. Is there an elegant solution > to this pattern in Rails? Any sample code from those of you who''ve > come across this and solved it before? > > Thank-you, > Duane Johnson > (canadaduane) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Duane Johnson
2005-Jan-21 16:24 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
Here''s an idea that seems to work in harmony with "the rails way": As an example, set up a controller to fetch a) A collection of records, and b) Two specific records: class PartialTestController < ApplicationController def index @people = Person.find_all @first_person = @people[0] @second_person = @people[1] if (@params[''submit'']) raise end end end Write a partial, such as _person.rhtml as follows: <% @person = person %> <%= text_field :person, "first_name", "index" => (index rescue person.id) %> <%= text_field :person, "last_name", "index" => (index rescue person.id) %><br> Then in a view such as index.rhtml, write: <%= form_tag %> <%= render_collection_of_partials "person", @people %> <br><br> <%= render_partial "person", @first_person, "index" => "first_person" %> <%= render_partial "person", @second_person, "index" => "second_person" %> <input type="submit" name="submit" value="Submit"> <%= end_form_tag %> The magic occurs inside of the partial: The class variable @person is assigned the value of the passed-in local variable, person (this is the default functionality of the render_partial method). This is done so that the text_field (and all similar form helper methods) will get and fill in the correct default values for each item in the collection. In the case of a collection of partials, there will be no "index" local variable passed in, so the actual index of each field will be the ID of each ActiveRecord object in the collection. This is really handy for doing updates in the controller. In the other case, where you want to use the partial for a specific ActiveRecord object (rather than a collection), the "index" local variable is passed in to the render_partial method. As can be seen in the _person partial, the index variable overrides the person.id field, thus if you have a specific object to render it will be available in the @params for the controller. Here is a capture of the @params: Parameters: {"submit"=>"Submit", "person"=>{"second_person"=>{"first_name"=>"Paul", "last_name"=>"Otterstrom"}, "1"=>{"first_name"=>"Duane", "last_name"=>"Johnson"}, "2"=>{"first_name"=>"Paul", "last_name"=>"Otterstrom"}, "first_person"=>{"first_name"=>"Duane", "last_name"=>"Johnson"}, "3"=>{"first_name"=>"John", "last_name"=>"Peterson"}, "4"=>{"first_name"=>"Ben", "last_name"=>"Hardin"}, "5"=>{"first_name"=>"Test", "last_name"=>""}}} Duane Johnson (canadaduane) On Tue, 18 Jan 2005 20:54:58 -0500, Brian L. <zorander-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''ve often wondered the same thing. Given the way that rails parses > CGI, you could make fields named people[2][firstname] and then update > Person.find(2) with the data in @params[''people''][2] (assuming you use > attr_protected in the appropriate places in your model, of course). > This assumes that 2 is the id. Then at the bottom you put your update > button. I''m kinda pulling this out of nowhere but I think it will > work. > > For simple ''click to remove'' buttons, I tend to use a link with a get > request to immediately remove the item. > > Brian > > > On Tue, 18 Jan 2005 17:57:43 -0700, Duane Johnson > <duane.johnson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > So I''ve finally figured out how to make simple and elegant > > Create/Read/Update/Delete events for a single object such as @person. > > But how do the Rails experts do it for a list of objects? > > > > For example: > > > > class TestController < ApplicationController > > def list > > @people = Person.find_all > > end > > end > > > > The corresponding list.rhtml view will elegantly display the array of > > Person objects. But what if each "Person" row in the view is > > editable? For example, the view I am imagining would generate a text > > box for the first name, last name and phone number, all the way down > > the list, and a check box next to each person to delete the > > corresponding person from the database. Is there an elegant solution > > to this pattern in Rails? Any sample code from those of you who''ve > > come across this and solved it before? > > > > Thank-you, > > Duane Johnson > > (canadaduane) > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > The years ahead pick up their dark bags. > They move closer. There''s a slight rise in the silence > > then nothing. > -- > (If you''re receiving this in response to mail sent to > bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, > but mail will be forwarded here indefinitely) >
Carl Youngblood
2005-Jan-21 23:06 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
Duane Johnson wrote:>The corresponding list.rhtml view will elegantly display the array of >Person objects. But what if each "Person" row in the view is >editable? For example, the view I am imagining would generate a text >box for the first name, last name and phone number, all the way down >the list, and a check box next to each person to delete the >corresponding person from the database. Is there an elegant solution >to this pattern in Rails? Any sample code from those of you who''ve >come across this and solved it before? >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. Carl
Joe Van Dyk
2005-Jan-21 23:17 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
On Fri, 21 Jan 2005 15:06:47 -0800, Carl Youngblood <carlwork-0CEYHQKyN7s@public.gmane.org> wrote:> Duane Johnson wrote: > > >The corresponding list.rhtml view will elegantly display the array of > >Person objects. But what if each "Person" row in the view is > >editable? For example, the view I am imagining would generate a text > >box for the first name, last name and phone number, all the way down > >the list, and a check box next to each person to delete the > >corresponding person from the database. Is there an elegant solution > >to this pattern in Rails? Any sample code from those of you who''ve > >come across this and solved it before? > > > 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. > > Carl > _______________________________________________I would love to see an implementation of that.
Reid Ellis
2005-Jan-22 06:54 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
On Jan 21, 2005, at 18:17, Joe Van Dyk wrote:> On Fri, 21 Jan 2005 15:06:47 -0800, Carl Youngblood <carlwork-0CEYHQKyN7s@public.gmane.org> > 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 would love to see an implementation of that.It may not be running on Rails, but http://flickr.com does exactly this kind of thing to edit fields all over the place. Reid
Jarkko Laine
2005-Jan-22 08:09 UTC
Re: A "Rails" way to design a list-like page with CRUD capabilities?
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 %> €</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) %> €</td> <td><input type="submit" value="Update" /></td> <%= end_form_tag() %> </tr> <% end %>> Carl > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails