RE: Model Composition in 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| %>


        <td><pre width="65"><%= priority.detail %></pre></td>


              <%= link_to 'Edit', :action => 'editItem', :id => %>

              <%= link_to 'Remove', :action => 'removeItem', :id => %>



  <% end %>


        <td colspan=2>

              <%= text_field 'p4_priorities', 'detail', :size => 75


              <%= link_to 'Add', :action => 'create', :id => @p4,

:params => {:contact_id => @p4.contact_id } %>



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


3500 Pelco Way

Clovis, CA 93612

Office - 800-289-9100

Email - removed_email_address@domain.invalid

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| %>
<% @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,

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|”