views:

14

answers:

1

Alright, I know my title is a little obscure but it best describes the problem I am having.

Essentially, I have a list of users, and want to be able to edit their information in-line using AJAX.

Since the users are showing up in rows, I am using a partial to render the data and the forms (which will be hidden initially by the ajax), however, when the rows are rendered currently only the last item has it's form's fields populated.

I suspect this has something to do with the fact that all the form fields have the same id's and it is confusing the DOM. But I don't know how to make sure the id's are unique.

Here is a small example:

In my view:

<%= render :partial => 'shared/user', :collection => @users %>

My partial (broke down to just the form) note that I am using the local variable "user"

<% form_for user, :html => {:multipart => true} do |f| -%>
<%= f.label :name, "Name*" %>
<%= f.text_field :title, :class => "input" %>
<%= f.label :Address, "Address" %>
<%= f.text_field :address, :class => "input" %>
<%= f.label :description, "Description*" %>
<%= f.text_area :description, :class => "input" %>
<% end -%>

When the html is rendered each form has a unique id (for the id of the user) but the elements themselves all have the same id, and only the last user form is actually getting populated with values.

Does anyone have any ideas?? :)

Thanks in advance!

+1  A: 

Alright, after having some lunch and regaining some brain cells, (and with a little help from Google) I figured this one out.

When passing a collection to a partial like this:

<%= render :partial => 'shared/user', :collection => @users %>

Rails creates a counter variable that you can use to define an index for the form in the form of "variable_counter":

<% form_for user, :index => user_counter, :html => {:multipart => true} do |f| -%>

This adds the index number to the form id as well as all the field id's and solved my little problem. :)

I hope this helps out someone else with this issue. :)

Dustin M.