Question : Rails: Replace HTML with a partial and a collection

I have a list of people that I am trying to edit.  I am trying to create a form that replaces the row when someone clicks on an ajax/prototype link.  Since I created the rows with a partial and collection I thought I could do the same with the form.  I tried it and I get this error:

NoMethodError (undefined method `each_with_index' for #<>):
    /Library/Ruby/Gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in `method_missing'

It says I am missing the method.  I am confused as to how this is happening.  I included my code below.

Thanks,
Grant
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
def peopleEditForm
	@pep = People.find(params[:id])
	row = "row_%d" % [params[:id]]
		
	render :update do |page|
		page.replace row, render(:partial=>'editPeople', :collection=> @pep)
	end
end
Open in New Window Select All

Answer : Rails: Replace HTML with a partial and a collection

If you're replacing a single row, you probably don't want to pass a collection.

Just to clarify:
People.find(1)
 => People object (singular! Not an array!)

People.find([1,2])  OR  People.find(:all, :conditions => ....)
 => Array of People objects

So, when you render a partial with a :collection, it's expecting an array.  It's calling each_with_index on a singular People object.

Probably, you want to use :object => @pep instead of :collection => @pep.
Random Solutions  
 
programming4us programming4us