# \[ANN\] REST in Place - RESTful Inplace-Editor

**URL:** <https://www.ruby-forum.com/t/ann-rest-in-place-restful-inplace-editor/124015>\
**Category:** Rails\
**Created:** [December 23, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/ann-rest-in-place-restful-inplace-editor/124015 "2007-12-23T15:10:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Java](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@Java](https://www.ruby-forum.com/u/Java)\
**Post date:** [December 23, 2007, 3:10pm UTC](https://www.ruby-forum.com/t/ann-rest-in-place-restful-inplace-editor/124015/1 "2007-12-23T15:10:27Z")

</div>

Hi,

since the old inplace\_editor plugin is pretty outdated, I decided to  
write a  
new one that uses a REST api and works seamlessly with RESTful rails  
controllers. Its main advantage is that you don’t need ANY special  
serverside  
code for this to work and any custom workflows in your controllers  
(authentication etc. ) apply to the inplace editor automatically.

This is my first Javascript Project, my first jQuery Project and my  
first Rails  
plugin, so I’d be interested in comments and/or suggestions for  
improvement.

# REST in Place

```
                                _______
                               / \
                               | R.I.P.|
                               | |
                               | |
                             -------------

```

“REST in Place” is an AJAX Inplace-Editor that talks to RESTful  
controllers.  
It requires absolutely no additional server-side code if your  
controller  
fulfills the following REST preconditions:

- It uses the HTTP PUT method to update a record
- It delivers an object in XML form for requests with  
“Accept: application/xml” headers

The editor works by PUTting the updated value to the server and  
GETting the  
updated record afterwards to display the updated value.  
That way any authentication methods or otherwise funky workflows in  
your  
controllers are used for the inplace-editors requests.

URL: [http://svn.varwig.org/rails/plugins/rest\_in\_place/](http://svn.varwig.org/rails/plugins/rest_in_place/)  
BLOG: [http://jan.varwig.org/](http://jan.varwig.org/)

# Instructions

REST in Place needs the [jQuery](http://www.jquery.com/) library to work.

Install REST in Place with

```
script/plugin install 

```

[http://svn.varwig.org/rails/plugins/rest\_in\_place/](http://svn.varwig.org/rails/plugins/rest_in_place/)

To make a piece of Text inplace-editable, wrap it into an element (a  
span  
usually) with class “rest\_in\_place”. The editor needs 3 pieces of  
information  
to work: a URL, an object name and the attribute name. These are  
provided as  
follows:

- put attributes into the element, like this:

attribute=“name”\>  
Johnny

- if any of these attributes is missing, DOM parents of the elemet  
are searched  
for them. That means you can write something like:

span\>  
  
eMail: removed\_email\_address@domain.invalid

- You can completely omit the url, the current documents url is  
taken then.  
With proper RESTful controllers this should always work, the url-  
attribute  
is for cases when you want to edit a resource that is displayed as  
part of  
a non-RESTful webpage.

- Rails provides the dom\_id helper that constructs a dom id out of  
an  
ActiveRecord for you. So, your HTML page may look like this:

span\>  
  
eMail: removed\_email\_address@domain.invalid

REST in Place recognizes dom\_ids of this form and derives the object  
parameter  
from them, so that (with the current documents url used) you really  
only need  
to provide the attributes name in most cases.

!! --------  
!! Note that a manually defined (in the element or in one of the  
parents)  
!! object always overrides dom\_id recognition.  
!! --------

# Example

Your routes.rb:

```
map.resources :users

```

Your app/controllers/users\_controller.rb:

```
class UsersController < ApplicationController
  def show
    @user = User.find params[:id]
    respond_to do |type|
      type.html
      type.xml {render :xml => @user.to_xml}
    end
  end

  def update
    @user = User.find params[:id]
    @user.update_attributes!(params[:user])
    redirect_to @user, :status => :see_other
  end
end

```

Your app/views/users/show.html.erb:

```
<html>
<head>
    <script type="text/javascript" src="/javascripts/

```

jquery-1.2.1.js" charset=“utf-8”\>

  
ID: \<%= @user.id %\>  
  
Name: \<%=  
 @user.name %\>  

  
  

# Why jQuery?

Why did I write this with jQuery instead of Prototype?

Frankly I never even worked with prototype and have just recently  
gotten into  
Javascript using jQuery. jQuery seems superior to Protoype and will  
hopefully  
replace Prototye someday or at least get [integrated into Rails]  
[jRails](http://ennerchi.com/projects/jrails) as nicely as  
Prototype so people can chose which framework they want to use.

Also, REST in Place at this stage is primarily a proof of concept.

# Non-Rails

REST in Place was written for Ruby on Rails but is usable with any  
kind of  
RESTful web api. Just include the rest\_in\_place.js in your webpage and  
follow  
the instructions.

Copyright (c) 2007 [Jan Varwig], released under the MIT license

---

<div class="post-metadata">

**Author:** ![Java](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/898d66/32.png) [@Java](https://www.ruby-forum.com/u/Java)\
**Post date:** [December 24, 2007, 1:08am UTC](https://www.ruby-forum.com/t/ann-rest-in-place-restful-inplace-editor/124015/2 "2007-12-24T01:08:44Z")

</div>

Whoops there goes my weekend.

I made a version for Prototype tonight that behaves exactly like the  
jQuery version.  
The plugin now contains both versions, you decide which one to include  
in your template.

You can find the updated README at  
[http://svn.varwig.org/rails/plugins/rest\_in\_place/README](http://svn.varwig.org/rails/plugins/rest_in_place/README)  
.

Merry Christmas

Jan Varwig
