Forum: Ruby on Rails RE: Model Composition in Rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
C687aa87c98c371044f84d28a72496ca?d=identicon&s=25 Brad Eck (bradeck)
on 2006-01-03 19:00
(Received via mailing list)
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 => %></p>



      <% end %>


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



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 -
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-04 01:31
(Received via mailing list)
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,
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-04 01:34
(Received via mailing list)
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|"

This topic is locked and can not be replied to.