views:

116

answers:

2

I have a bit of javascript magic going on to create and hide divs on the fly as the user interacts with the page. Each div contains a view of some part of my application and each model instance in these views has an owner. If the person browsing the page is NOT the owner, they just see the data. However, if the user is the owner of the data, they get an edit view.

It might be possible to stash off the state of all the divs and re-render the page exactly as it was when the user submits changes, but I'd rather just have some quick-and-easy way to have the transaction be something along the lines of: user hits submit button, server processes changes and sends back confirmation, minor change on screen flashes the notification of the completion of the transaction.

Anyone have a pointer to a working example? The threshhold for implementation is going to be whether it is easier to do what I'm asking about here or just bite the bullet and carry the div state through the transaction.

A: 

Sounds like all you need is some Ajax magic. Have a look at remote_form_for or any of the other Prototype Helpers.

On Freund
+3  A: 

As On Freund noted, remote_form_for is the way to go. Couple that with a dash of RJS and you're off and running:

# Form
- remote_form_for(@obj, :url => obj_path(@obj)) do |f|

  = f.text_field :some_attribute
  = f.submit "Submit"


# Controller action

def edit
  respond_to do |wants|
    wants.html { }
    wants.js {
      render :update do |page|
        page.replace_html :some_div, "Some content"
      end
    }
  end
end
Matt Darby