I''m struggling understanding the model view of a combined model. Take the following: class P4 < ActiveRecord::Base has_many :p4_priorities end I figured out the naming and referencing of the sub models (i.e. P4.p4_priorities - seems simple enough but I had weird issues related to it). I''m all ok except that I am having issues with how to reference a ''sub-model'' in a form field. This is what I have: <%= text_field ''p4_p4_priorities'', ''detail'', :size => 75 %> No errors - it''s just not saving which tells me the framework isn''t understanding what to save. Can you help? Also, the models associated with the related tables have validation but that doesn''t apparently roll up into the combined edit I am providing. Is that expected? If so, how do I reference those fields in validation in this model? Just a typical dereference as mentioned above? _______________________ Brad Eck Sr. Software Engineer Pelco 3500 Pelco Way Clovis, CA 93612 Office - 800-289-9100 Email - beck-UGS+yiLUcO4AvxtiuMwx3w@public.gmane.org <BLOCKED::mailto:beck-UGS+yiLUcO4AvxtiuMwx3w@public.gmane.org> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/29/05, Eck, Brad <BEck@pelco.com> wrote:> > > > I'm struggling understanding the model view of a combined model. Take the > following: > > > > class P4 < ActiveRecord::Base > > has_many :p4_priorities > > end > > > > I figured out the naming and referencing of the sub models (i.e. > P4.p4_priorities – seems simple enough but I had weird issues related to > it). I'm all ok except that I am having issues with how to reference a > 'sub-model' in a form field. This is what I have: > > > > <%= text_field 'p4_p4_priorities', 'detail', :size => 75 %> > > No errors – it's just not saving which tells me the framework isn't > understanding what to save. Can you help? > > Also, the models associated with the related tables have validation but that > doesn't apparently roll up into the combined edit I am providing. Is that > expected? If so, how do I reference those fields in validation in this > model? Just a typical dereference as mentioned above? >A text field contains a single string of text, but you're trying to call it on a collection of things. That's equivalent to saying: <%= text_field 'all_rock_musicians_in_the_world', 'firstname' %>, which doesn't really make sense. Your best bet is to put this in a partial form, and call it with: render(:partial => 'priority', :collection => @p4.p4_priorities) The partial would then have text_field, etc, etc, to deal with a single priority: <% @priority = priority -%> (The helpers require instance variables.) <%= text_field 'priority', 'detail', :index => priority_counter, :size => 75 %> When you're done, you'll have a numbered array of those fields, one per "p4_priority" _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hmmm - thanks Wilson. Ya know it''s interesting because I am actually using the text entry only for new items. My current partial looks something like this: <% @p4.p4_priorities.each do |priority| %> <tr> <td><pre width="65"><%= priority.detail %></pre></td> <td> <%= link_to ''Edit'', :action => ''editItem'', :id => priority.id %> <%= link_to ''Remove'', :action => ''removeItem'', :id => priority.id %></p> </td> </tr> <% end %> <tr> <td colspan=2> <%= text_field ''p4_priorities'', ''detail'', :size => 75 %> <!-- this is the line in question... --> <%= link_to ''Add'', :action => ''create'', :id => @p4, :params => {:contact_id => @p4.contact_id } %> </td> </tr> So, the list of data is dropping out fine, but trying to figure out what I should actually be handing the text field is a little odd. Should I do a new on the p4_priorities collection and mark it with that ''priority'' or should I approach it some other way? _______________________ Brad Eck Sr. Software Engineer Pelco 3500 Pelco Way Clovis, CA 93612 Office - 800-289-9100 Email - beck@pelco.com -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich Sent: Thursday, December 29, 2005 3:12 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Model Composition in Rails On 12/29/05, Eck, Brad <BEck@pelco.com> wrote:>>>> I''m struggling understanding the model view of a combined model. Takethe> following:>>>> class P4 < ActiveRecord::Base>> has_many :p4_priorities>> end>>>> I figured out the naming and referencing of the sub models (i.e.> P4.p4_priorities - seems simple enough but I had weird issues relatedto> it). I''m all ok except that I am having issues with how to reference a> ''sub-model'' in a form field. This is what I have:>>>> <%= text_field ''p4_p4_priorities'', ''detail'', :size => 75%>>> No errors - it''s just not saving which tells me the framework isn''t> understanding what to save. Can you help?>> Also, the models associated with the related tables have validationbut that> doesn''t apparently roll up into the combined edit I am providing. Isthat> expected? If so, how do I reference those fields in validation in this> model? Just a typical dereference as mentioned above?>A text field contains a single string of text, but you''re trying to call it on a collection of things. That''s equivalent to saying: <%= text_field ''all_rock_musicians_in_the_world'', ''firstname'' %>, which doesn''t really make sense. Your best bet is to put this in a partial form, and call it with: render(:partial => ''priority'', :collection => @p4.p4_priorities) The partial would then have text_field, etc, etc, to deal with a single priority: <% @priority = priority -%> (The helpers require instance variables.) <%= text_field ''priority'', ''detail'', :index => priority_counter, :size => 75 %> When you''re done, you''ll have a numbered array of those fields, one per "p4_priority" -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060103/4783ce1c/attachment-0001.html
Basically you want your text_field() invocation to be inside the "each" block for p4_priorities. It needs the "detail" option to make sense, and since detail() is an instance method of P4Priority, it can''t operate on a collection of them. It has to be a one-to-one text_field() to @priority relationship. So, right under this line: <% @p4.p4_priorities.each do |priority| %> ..add: <% @priority = priority -%> This is necessary because the form helpers, such as text_field, require the first parameter to be the name of an instance variable, not a local variable. Then, somewhere inside the ''each'' block, before you close it: <%= text_field ''priority'', ''detail'', :size => 75 %> Good luck, --Wilson. On 1/3/06, Eck, Brad <BEck@pelco.com> wrote:> > > > Hmmm - thanks Wilson. Ya know it''s interesting because I am actually using > the text entry only for new items. My current partial looks something like > this: > > > > <% @p4.p4_priorities.each do |priority| %> > > <tr> > > <td><pre width="65"><%= priority.detail %></pre></td> > > <td> > > <%= link_to ''Edit'', :action => ''editItem'', :id => > priority.id %> > > <%= link_to ''Remove'', :action => ''removeItem'', :id => > priority.id %></p> > > </td> > > </tr> > > <% end %> > > <tr> > > <td colspan=2> > > <%= text_field ''p4_priorities'', ''detail'', :size => 75 %> > <!-- this is the line in question... ? > > <%= link_to ''Add'', :action => ''create'', :id => @p4, > :params => {:contact_id => @p4.contact_id } %> > > </td> > > </tr> > > > > So, the list of data is dropping out fine, but trying to figure out what I > should actually be handing the text field is a little odd. Should I do a new > on the p4_priorities collection and mark it with that ''priority'' or should I > approach it some other way? > > > > _______________________ > > Brad Eck > > Sr. Software Engineer > > Pelco > > 3500 Pelco Way > > Clovis, CA 93612 > > Office - 800-289-9100 > > Email - beck@pelco.com > > > > > > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > Wilson Bilkovich > Sent: Thursday, December 29, 2005 3:12 PM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] Model Composition in Rails > > > > On 12/29/05, Eck, Brad <BEck@pelco.com> wrote: > > > > > > > > > > > > I''m struggling understanding the model view of a combined model. Take the > > > following: > > > > > > > > > > > > class P4 < ActiveRecord::Base > > > > > > has_many :p4_priorities > > > > > > end > > > > > > > > > > > > I figured out the naming and referencing of the sub models (i.e. > > > P4.p4_priorities ? seems simple enough but I had weird issues related to > > > it). I''m all ok except that I am having issues with how to reference a > > > ''sub-model'' in a form field. This is what I have: > > > > > > > > > > > > <%= text_field ''p4_p4_priorities'', ''detail'', :size => 75 %> > > > > > > No errors ? it''s just not saving which tells me the framework isn''t > > > understanding what to save. Can you help? > > > > > > Also, the models associated with the related tables have validation but > that > > > doesn''t apparently roll up into the combined edit I am providing. Is that > > > expected? If so, how do I reference those fields in validation in this > > > model? Just a typical dereference as mentioned above? > > > > > > > A text field contains a single string of text, but you''re trying to > > call it on a collection of things. That''s equivalent to saying: > > <%= text_field ''all_rock_musicians_in_the_world'', > ''firstname'' %>, > > which doesn''t really make sense. > > > > Your best bet is to put this in a partial form, and call it with: > > render(:partial => ''priority'', :collection => @p4.p4_priorities) > > > > The partial would then have text_field, etc, etc, to deal with a > > single priority: > > <% @priority = priority -%> (The helpers require instance variables.) > > <%= text_field ''priority'', ''detail'', :index => priority_counter, :size => 75 > %> > > > > When you''re done, you''ll have a numbered array of those fields, one > > per "p4_priority" > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
Oh, and (replying to myself), if you really want there to be multiple "priority detail" entries on the page, you need to differentiate them with the :index => some_integer option. The usual way to do this is via render(:partial => ''priority'', :collection => @p4.p4_priorities) You would then have _priority.rhtml as a partial form, and "priority_counter" could be created automatically inside the partial, for use as an index. Another way to do it, without a partial, would be to use "each_with_index do |priority, i|" instead of "each do |priority|" --Wilson. On 1/3/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:> Basically you want your text_field() invocation to be inside the > "each" block for p4_priorities. It needs the "detail" option to make > sense, and since detail() is an instance method of P4Priority, it > can''t operate on a collection of them. It has to be a one-to-one > text_field() to @priority relationship. > > So, right under this line: > <% @p4.p4_priorities.each do |priority| %> > ..add: > <% @priority = priority -%> > This is necessary because the form helpers, such as text_field, > require the first parameter to be the name of an instance variable, > not a local variable. > > Then, somewhere inside the ''each'' block, before you close it: > <%= text_field ''priority'', ''detail'', :size => 75 %> > > Good luck, > --Wilson. > > On 1/3/06, Eck, Brad <BEck@pelco.com> wrote: > > > > > > > > Hmmm - thanks Wilson. Ya know it''s interesting because I am actually using > > the text entry only for new items. My current partial looks something like > > this: > > > > > > > > <% @p4.p4_priorities.each do |priority| %> > > > > <tr> > > > > <td><pre width="65"><%= priority.detail %></pre></td> > > > > <td> > > > > <%= link_to ''Edit'', :action => ''editItem'', :id => > > priority.id %> > > > > <%= link_to ''Remove'', :action => ''removeItem'', :id => > > priority.id %></p> > > > > </td> > > > > </tr> > > > > <% end %> > > > > <tr> > > > > <td colspan=2> > > > > <%= text_field ''p4_priorities'', ''detail'', :size => 75 %> > > <!-- this is the line in question... ? > > > > <%= link_to ''Add'', :action => ''create'', :id => @p4, > > :params => {:contact_id => @p4.contact_id } %> > > > > </td> > > > > </tr> > > > > > > > > So, the list of data is dropping out fine, but trying to figure out what I > > should actually be handing the text field is a little odd. Should I do a new > > on the p4_priorities collection and mark it with that ''priority'' or should I > > approach it some other way? > > > > > > > > _______________________ > > > > Brad Eck > > > > Sr. Software Engineer > > > > Pelco > > > > 3500 Pelco Way > > > > Clovis, CA 93612 > > > > Office - 800-289-9100 > > > > Email - beck@pelco.com > > > > > > > > > > > > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > > Wilson Bilkovich > > Sent: Thursday, December 29, 2005 3:12 PM > > To: rails@lists.rubyonrails.org > > Subject: Re: [Rails] Model Composition in Rails > > > > > > > > On 12/29/05, Eck, Brad <BEck@pelco.com> wrote: > > > > > > > > > > > > > > > > > > > > I''m struggling understanding the model view of a combined model. Take the > > > > > following: > > > > > > > > > > > > > > > > > > > > class P4 < ActiveRecord::Base > > > > > > > > > > has_many :p4_priorities > > > > > > > > > > end > > > > > > > > > > > > > > > > > > > > I figured out the naming and referencing of the sub models (i.e. > > > > > P4.p4_priorities ? seems simple enough but I had weird issues related to > > > > > it). I''m all ok except that I am having issues with how to reference a > > > > > ''sub-model'' in a form field. This is what I have: > > > > > > > > > > > > > > > > > > > > <%= text_field ''p4_p4_priorities'', ''detail'', :size => 75 %> > > > > > > > > > > No errors ? it''s just not saving which tells me the framework isn''t > > > > > understanding what to save. Can you help? > > > > > > > > > > Also, the models associated with the related tables have validation but > > that > > > > > doesn''t apparently roll up into the combined edit I am providing. Is that > > > > > expected? If so, how do I reference those fields in validation in this > > > > > model? Just a typical dereference as mentioned above? > > > > > > > > > > > > > A text field contains a single string of text, but you''re trying to > > > > call it on a collection of things. That''s equivalent to saying: > > > > <%= text_field ''all_rock_musicians_in_the_world'', > > ''firstname'' %>, > > > > which doesn''t really make sense. > > > > > > > > Your best bet is to put this in a partial form, and call it with: > > > > render(:partial => ''priority'', :collection => @p4.p4_priorities) > > > > > > > > The partial would then have text_field, etc, etc, to deal with a > > > > single priority: > > > > <% @priority = priority -%> (The helpers require instance variables.) > > > > <%= text_field ''priority'', ''detail'', :index => priority_counter, :size => 75 > > %> > > > > > > > > When you''re done, you''ll have a numbered array of those fields, one > > > > per "p4_priority" > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >
:index was the magic. It''s odd as I seem to have to reference the index as a string in my controller but it works so I am happy! Thank you!!! _______________________ Brad Eck Sr. Software Engineer Pelco 3500 Pelco Way Clovis, CA 93612 Office - 800-289-9100 Email - beck@pelco.com -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Wilson Bilkovich Sent: Tuesday, January 03, 2006 4:32 PM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Model Composition in Rails Oh, and (replying to myself), if you really want there to be multiple "priority detail" entries on the page, you need to differentiate them with the :index => some_integer option. The usual way to do this is via render(:partial => ''priority'', :collection => @p4.p4_priorities) You would then have _priority.rhtml as a partial form, and "priority_counter" could be created automatically inside the partial, for use as an index. Another way to do it, without a partial, would be to use "each_with_index do |priority, i|" instead of "each do |priority|" --Wilson. On 1/3/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:> Basically you want your text_field() invocation to be inside the > "each" block for p4_priorities. It needs the "detail" option to make > sense, and since detail() is an instance method of P4Priority, it > can''t operate on a collection of them. It has to be a one-to-one > text_field() to @priority relationship. > > So, right under this line: > <% @p4.p4_priorities.each do |priority| %> > ..add: > <% @priority = priority -%> > This is necessary because the form helpers, such as text_field, > require the first parameter to be the name of an instance variable, > not a local variable. > > Then, somewhere inside the ''each'' block, before you close it: > <%= text_field ''priority'', ''detail'', :size => 75 %> > > Good luck, > --Wilson. > > On 1/3/06, Eck, Brad <BEck@pelco.com> wrote: > > > > > > > > Hmmm - thanks Wilson. Ya know it''s interesting because I am actually using > > the text entry only for new items. My current partial looks something like > > this: > > > > > > > > <% @p4.p4_priorities.each do |priority| %> > > > > <tr> > > > > <td><pre width="65"><%= priority.detail %></pre></td> > > > > <td> > > > > <%= link_to ''Edit'', :action => ''editItem'', :id => > > priority.id %> > > > > <%= link_to ''Remove'', :action => ''removeItem'', :id => > > priority.id %></p> > > > > </td> > > > > </tr> > > > > <% end %> > > > > <tr> > > > > <td colspan=2> > > > > <%= text_field ''p4_priorities'', ''detail'', :size => 75 %> > > <!-- this is the line in question... ? > > > > <%= link_to ''Add'', :action => ''create'', :id => @p4, > > :params => {:contact_id => @p4.contact_id } %> > > > > </td> > > > > </tr> > > > > > > > > So, the list of data is dropping out fine, but trying to figure out what I > > should actually be handing the text field is a little odd. Should I do a new > > on the p4_priorities collection and mark it with that ''priority'' or should I > > approach it some other way? > > > > > > > > _______________________ > > > > Brad Eck > > > > Sr. Software Engineer > > > > Pelco > > > > 3500 Pelco Way > > > > Clovis, CA 93612 > > > > Office - 800-289-9100 > > > > Email - beck@pelco.com > > > > > > > > > > > > > > -----Original Message----- > > From: rails-bounces@lists.rubyonrails.org > > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of > > Wilson Bilkovich > > Sent: Thursday, December 29, 2005 3:12 PM > > To: rails@lists.rubyonrails.org > > Subject: Re: [Rails] Model Composition in Rails > > > > > > > > On 12/29/05, Eck, Brad <BEck@pelco.com> wrote: > > > > > > > > > > > > > > > > > > > > I''m struggling understanding the model view of a combined model. Take the > > > > > following: > > > > > > > > > > > > > > > > > > > > class P4 < ActiveRecord::Base > > > > > > > > > > has_many :p4_priorities > > > > > > > > > > end > > > > > > > > > > > > > > > > > > > > I figured out the naming and referencing of the sub models (i.e. > > > > > P4.p4_priorities - seems simple enough but I had weird issues related to > > > > > it). I''m all ok except that I am having issues with how to reference a > > > > > ''sub-model'' in a form field. This is what I have: > > > > > > > > > > > > > > > > > > > > <%= text_field ''p4_p4_priorities'', ''detail'', :size => 75 %> > > > > > > > > > > No errors - it''s just not saving which tells me the framework isn''t > > > > > understanding what to save. Can you help? > > > > > > > > > > Also, the models associated with the related tables have validation but > > that > > > > > doesn''t apparently roll up into the combined edit I am providing. Is that > > > > > expected? If so, how do I reference those fields in validation in this > > > > > model? Just a typical dereference as mentioned above? > > > > > > > > > > > > > A text field contains a single string of text, but you''re trying to > > > > call it on a collection of things. That''s equivalent to saying: > > > > <%= text_field ''all_rock_musicians_in_the_world'', > > ''firstname'' %>, > > > > which doesn''t really make sense. > > > > > > > > Your best bet is to put this in a partial form, and call it with: > > > > render(:partial => ''priority'', :collection => @p4.p4_priorities) > > > > > > > > The partial would then have text_field, etc, etc, to deal with a > > > > single priority: > > > > <% @priority = priority -%> (The helpers require instance variables.) > > > > <%= text_field ''priority'', ''detail'', :index => priority_counter, :size => 75 > > %> > > > > > > > > When you''re done, you''ll have a numbered array of those fields, one > > > > per "p4_priority" > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > >