Mariano Kamp
2006-Mar-18 15:47 UTC
[Rails] Need help .. Rails Recipes, in_place_editor, selecting associations
Hi, I am currently reading the Rails Recipes book and like the chapter on providing your own in_place_editor for enumerations / selects. I understood so far, or I believe that I understood ;-), how to do that for a field that uses values only, like choosing from "red", "black", "green". What I fail to understand is how to chose from associated objects?! Consider the following example. A "Reader" has_one "Book", the book s/he is currently reading. Both "Reader" and "Book" are model objects. How would you go about at view to make the current book selectable? With above mentioned Rails Recipe "Making Your Own JavaScript Helper" it is trivial to make the IDs (book_id) selectable, but I would rather like the user to select from a list of book titles than from a list of book ids. If I provide the in_place_editor with the ids and titles of the available book as select options it works on the surface (book titles can be chosen, ids are used for the update calls), but the current value displayed in the tag is still an id. And I believe that I have to provide the in_place_editor helper method with ":reader" and ":book_id", because it uses it for the update_url. Providing a new parameter to the helper method for creating the in_place_editor might work, but it seems that there is some stuff happening under the hood retrieving the original id when creating the instance tag. Would it be an option to overwrite the result? If so how to find which attribute? tag.inspect returns pages of variables. What to look for? Btw. What is the easiest way to open a referenced class, like InstanceTag, from within TextMate, when you don''t know what the source file is a called? Is a global search in the project the only option? I am at loss here what to do without rewriting most of the existing code relevant to in_place_editors? On the other hand though handling of associations in the view is also absent in the generated scaffolds and I haven''t seen any support for that in any other view related topic. Am I doing something wrong and don''t understand the Golden Path(tm)? Isn''t choosing from associated objects an every day case? Any help and insight is appreciated. Thanks, Mariano -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/d4fb6ce7/attachment-0001.html
Mariano Kamp
2006-Mar-19 15:49 UTC
[Rails] Re: Need help .. Rails Recipes, in_place_editor, selecting associations
I am still looking for help ;-) Regarding the question if rails turns a blind eye on associations handling in the view or me on the rails doc, I came across the following in the documentation of ActionView::Helpers::ActiveRecordHelper ( http://edgedocs.planetargon.org/classes/ActionView/Helpers/ActiveRecordHelper.html ): [..] The most far-reaching is the form method that creates a complete form for all the basic content types of the record --> (not associations or aggregations, though) <--. [..] So the rails helpers don''t provide support for assocations, right? Any idea why that is? Just out of curiosity. Imagine I would be capable and willing to implement the "missing" feature. How would I go about this and what would be the basic idea about doing this following the Golden Path(tm)?>From my point of view a) an identifier/label needs to be identified in theassociated model, e.g. "name" of the associated "Book". My first guess is that it belongs to the controller/view not the model of "Reader". Reader: belongs_to :book ReadersController: in_place_edit_for :reader, :name in_place_association_edit_for :reader, :book # <---------------- The following information should be derived: id field = book_id (as a default, otherwise the attribute would need to be specified) label field = book.name (as a default, otherwise the attribute would need to be specified) I am not sure if the label field is necessary at all, because the controller just needs to know which field to update, right? It might only be necessary if the view only handles the lables. More on that below. In the view: in_place_select_association_editor_field :reader, :book I believe it would be ok to make the label (book.name) unique. That would be convenient, because then "id" and "label/name" can be used interchangeable, i.e. just one, the lagel, need to be used. Otherwise all ids and labels/names of "Book" needs to be known in the view. The view should display a picklist with all the labels in it and depending on either using unique labels just have a label picked or when using ids and labels set the id in the view. Cheers, Mariano On 3/18/06, Mariano Kamp <mariano.kamp@gmail.com> wrote:> > Hi, > > I am currently reading the Rails Recipes book and like the chapter on > providing your own in_place_editor for enumerations / selects. > > I understood so far, or I believe that I understood ;-), how to do that > for a field that uses values only, like choosing from "red", "black", > "green". What I fail to understand is how to chose from associated objects?! > > > Consider the following example. A "Reader" has_one "Book", the book s/he > is currently reading. Both "Reader" and "Book" are model objects. How would > you go about at view to make the current book selectable? > > With above mentioned Rails Recipe "Making Your Own JavaScript Helper" it > is trivial to make the IDs (book_id) selectable, but I would rather like the > user to select from a list of book titles than from a list of book ids. > > If I provide the in_place_editor with the ids and titles of the > available book as select options it works on the surface (book titles can be > chosen, ids are used for the update calls), but the current value displayed > in the tag is still an id. And I believe that I have to provide the > in_place_editor helper method with ":reader" and ":book_id", because it uses > it for the update_url. > > Providing a new parameter to the helper method for creating the > in_place_editor might work, but it seems that there is some stuff happening > under the hood retrieving the original id when creating the instance tag. > Would it be an option to overwrite the result? If so how to find which > attribute? tag.inspect returns pages of variables. What to look for? > > Btw. What is the easiest way to open a referenced class, like > InstanceTag, from within TextMate, when you don''t know what the source file > is a called? Is a global search in the project the only option? > > I am at loss here what to do without rewriting most of the existing code > relevant to in_place_editors? > > On the other hand though handling of associations in the view is also > absent in the generated scaffolds and I haven''t seen any support for that in > any other view related topic. Am I doing something wrong and don''t > understand the Golden Path(tm)? Isn''t choosing from associated objects an > every day case? > > Any help and insight is appreciated. > > Thanks, > Mariano >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060319/b59de9d0/attachment-0001.html