Topic: Ajax Form for in-place editing

okay one more design question ..

I have a page with a list of some objects each with edit link and
a form to create a new object through an Ajax call.

If I click on the edit link an ajax request fetches the form to edit
this object.

It works, but I wanted to ask if this is the right way of doing this:

1) I have a partial _form.rhtml for both, the new and the edit form, this
is done with remote_form_for obj and it either gets obj.new or the related object and
builts a put or post form.

2) I added a route :member { :formfor => :post } and use a link_to_remote to this
method (with the obj ID) and a .rjs for this method inserts the form.
I could also use :member { :edit => :post } to use the edit method for this if I'm right..
But I'm not sure if this is the correct way to do this.

3) since I use the same _form.rhtml for both forms, new and edit, and both forms are displayed
at the same time on the page when editing an object, how can I somehow create different
ID's for each form?
Something like a dynamic id parameter in _form.rhtml ..


if someone has some hints or corrections, thank you
Christoph

Re: Ajax Form for in-place editing

Christoph wrote:

2) I added a route :member { :formfor => :post } and use a link_to_remote to this
method (with the obj ID) and a .rjs for this method inserts the form.
I could also use :member { :edit => :post } to use the edit method for this if I'm right..
But I'm not sure if this is the correct way to do this.

I wouldn't create a new route for this. The "edit" action is already designed to display an edit form - here you are just doing it through RJS. I recommend having the link send a "get" request to the "edit" action. You can use respond_to in edit action to respond appropriately. You can create an edit.rjs file to handle the displaying of the form partial.

Christoph wrote:

3) since I use the same _form.rhtml for both forms, new and edit, and both forms are displayed
at the same time on the page when editing an object, how can I somehow create different
ID's for each form?
Something like a dynamic id parameter in _form.rhtml ..

It is common to share the fields with the new/edit forms, but not the form itself (unless you are using simple_helpful plugin).

I recommend using two partials here: "_edit_form.rhtml" and "_fields.rhtml". The edit form would render the fields partial. The "new" form would just render the fields partial. This way the "id" of the form can have the id of the model you are editing in it to make it unique. Does that make sense?

Railscasts - Free Ruby on Rails Screencasts

Re: Ajax Form for in-place editing

I'm sorry, I'm using simply helpful but thought that behaviour of generating
method put/post automatically based on the related object, is builtin.
Somehow it just did not generate an ID.

I changed the route also.
Thanks