Hi Jay,

On Tue, 2009-08-18 at 07:32 -0700, jheaslip wrote:
> OK, here's an example of what I'm trying to do.
> 
> _form.html.erb
> 
> <%= form.error_messages %>
> <p>
>   <%= form.label :title %><br />
>   <%= form.text_field :title %>
> </p>
> <p>
>   <%= form.label :due_date %><br />
>   <%= form.datetime_select :due_date %>
> </p>
> <div id="tasks">
>   <h3>Tasks</h3>
>   <% form.fields_for :tasks do |task_form| -%>
>     <%= render :partial => 'task', :locals => { :form => task_form }
> %>
>   <% end -%>
> </div>
> <%= add_task_link(form) %>
> <p>
>   <%= form.submit 'Save' %>
> </p>
> 
> And here's the partial that gets called above (_task.html.erb)
> 
> <div class="task">
>   <p>
>     <%= form.label :name %>
>     <%= form.text_field :name, :size => 15 %>
>     <%= remove_task_link( form ) %>
>   </p>
>   <%= observe_field :project_tasks_attributes_0_name, :function =>
> 'alert("field changed")' %>
> </div>
> 
> This doesn't work because I need to change the observe_field name
> (:project_tasks_attributes_0_name) for each task.  If I had 3 tasks,
> the
> then the observe_field names would be:
> :project_tasks_attributes_0_name
> :project_tasks_attributes_1_name
> :project_tasks_attributes_2_name
> 
> I was wondering if there is a way to access the index of the task form
> loop to generate field names like the above.

Good explanation.  Thanks.  A couple of suggestions re: approach.  The
first probably won't work but it would be interesting to know.  I wonder
if 

do |task_form|

could be replaced with 

each_with_index |task_form, i|

both do and each take blocks. Probably won't work, but...

If you're sure of the naming / order, then the easiest way would be
something like (not tested)...

<% index = 0 %>
<% form.fields_for :tasks do |task_form| -%>
 <%= render :partial => 'task', :locals => { :form => task_form, :index => 
index } %>
  <% index += 1 %>
<% end -%>

And then in the partial, using a string instead of a symbol...

<%= observe_field "project_tasks_attributes_#{index}_name" ...

Not terribly rubyish, I know, but something on that order should be
workable ;-)

HTH,
Bill

> Thanks,
> Jay
> 
> On Aug 17, 9:35 pm, bill walton <bwalton...@gmail.com> wrote:
> > Hi Jay,
> >
> > On Mon, 2009-08-17 at 14:24 -0700, Jay wrote:
> > > I'm trying to observe a field that get generated inside a fields_for
> > > loop (I'm trying to create the form dynamically depending on a
> > > selection value).  Is there a way to access the index in the field_for
> > > loop?
> >
> > What do you mean by 'index'?  The observe_field method takes a DOM id.
> > If you're having trouble, post the view and the page source that
> > results. It's very difficult to help with such sparse information.
> >
> > Best regards,
> > Bill
> > 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to