[Rails] A "Rails" way to design a list-like page with CRUD
capabilities?
Duane Johnson
duane.johnson at gmail.com
Fri Jan 21 16:24:01 GMT 2005
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 at gmail.com> 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 at gmail.com> 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 at lists.rubyonrails.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 at andrew.cmu.edu, don't be concerned This is my new address,
> but mail will be forwarded here indefinitely)
>
More information about the Rails
mailing list