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

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

        </td>

  </tr>

  <% end %>

  <tr>

        <td colspan=2>

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

%>

              <%= 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 - [email protected]

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.

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.