forked from janv/rest_in_place
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
168 lines (128 loc) · 5.88 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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 JSON form for requests with
"Accept: application/javascript" 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://github.com/janv/rest_in_place/>
REPOSITORY: <git://github.com/janv/rest_in_place.git>
BLOG: <http://jan.varwig.org/projects/rest-in-place>
Instructions
============
First, install REST in Place with
script/plugin install git://github.com/janv/rest_in_place.git
To use it, include either rest_in_place.js or jquery.rest_in_place.js in your
template (_after_ loading the framework's JavaScript ). rest_in_place.js is the
version for the Prototype framework, jquery.rest_in_place.js uses the
[jQuery][] framework.
For REST in Place to work with Rails request forgery protection, place the
following lines into your applications layout:
<script type="text/javascript">
rails_authenticity_token = '<%= form_authenticity_token %>'
</script>
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:
<span class="rest_in_place" url="/users/1" object="user" attribute="name">
<%= @user.name %>
</span>
- if any of these attributes is missing, DOM parents of the element are searched
for them. That means you can write something like:
<div object="user" url="/users/1">
Name: <span class="rest_in_place" attribute="name" ><%= @user.name %></span><br/>
eMail: <span class="rest_in_place" attribute="email"><%= @user.email %></span>
</div>
- You can completely omit the url, to use the current document's url.
With proper RESTful controllers this should always work, the explicit
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:
<div id="<%= dom_id @user # == "user_1" %>">
Name: <span class="rest_in_place" attribute="name" ><%= @user.name %></span><br/>
eMail: <span class="rest_in_place" attribute="email"><%= @user.email %></span>
</div>
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.
!! --------
[jQuery]: http://www.jquery.com/
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.js {render :json => @user}
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>
<%= javascript_include_tag "jquery-1.2.6.min" , "jquery.rest_in_place" %>
<script type="text/javascript">
rails_authenticity_token = '<%= form_authenticity_token %>'
</script>
</head>
<body>
<div id="<%= dom_id @user %>">
ID: <%= @user.id %><br />
Name: <span class="rest_in_place" attribute="name"><%= @user.name %></span>
</div>
</body>
</html>
You can run this example by switching to the testapp included in this
plugin, running script/server (sqlite3 required) and going to
localhost:3000/users/1
Currently only the jQuery version is tested in the testapp.
Why jQuery?
===========
Why did I write this with jQuery instead of Prototype?
(Note: The very first version only had the jQuery version included. Immediately
after releasing the initial version I tried porting the plugin to Prototype.
I was successful but Prototype really isn't as much fun to use as jQuery.)
Frankly I never even worked with prototype and have just recently gotten into
Javascript using jQuery. jQuery is superior to Protoype and will hopefully
replace Prototye someday or at least get [integrated into Rails][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.
[jRails]: http://ennerchi.com/projects/jrails
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.
Participation
=============
I'd love to get comments, bug reports (or better, patches) about REST in Place.
I haven't set up a trac yet, so please use the project page at my blog
to contact me about REST in Place:
<http://jan.varwig.org/projects/rest-in-place>
Copyright (c) 2008 [Jan Varwig], released under the MIT license