In_place_editor with a collection in a partial in Ruby on Rails
It seems like it would take a lot of work to get the in_place_editor to work in a partial on a collection, but it does. (It took me a lot of time to figure this out, but maybe I’m just more than average dense.) The best post to-date on this is at we eat bricks.
Just add the usual in the controller (user_controller.rb):
in_place_edit_for :user, :name
And, of course, the method:
def edit @users = User.find(:all) end
Then, in the main view (edit.rhtml):
<%= render :partial=>'user', :collection=>@users %>
Then, in the partial (_users.rhtml):
<%= in_place_editor_field :model, :column %>
At first, this won’t work. You will get an error that says “Called id for nil, which would mistakenly be 4 — if you really wanted the id of nil, use object_id” on the line containing the “in_place_editor_field”.
It turns out this is a bug, and there is a workaround. Just add this at the top of your partial:
<%- @user = user -%>
I hope this helps someone save some time.
October 31st, 2007 at 5:04 pm
Thanks for the mention:-)
Looks like we both are exploring innards of Ruby on Rails. I’ll be sure to keep an eye on your blog.
Best of luck!
Jamie
January 16th, 2008 at 1:13 pm
Thanks v much for this - saved me hours :):):)
June 17th, 2008 at 5:40 pm
Thanks! This helped a LOT!
October 10th, 2008 at 9:27 am
I saved a lot of time !!! Thanks