Skip to content

Instantly share code, notes, and snippets.

@sinisterchipmunk
Created January 27, 2012 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinisterchipmunk/1689251 to your computer and use it in GitHub Desktop.
Save sinisterchipmunk/1689251 to your computer and use it in GitHub Desktop.
Code examples for "Understanding REST in Rails 3" - http://thoughtsincomputation.com/understanding-rest-in-rails-3
REST request | CRUD request | Action
----------------|----------------------|-------
POST /posts | /posts/create | Create
GET /posts/1 | /posts/show/colin | Read
PUT /posts/1 | /posts/update/colin | Update
DELETE /posts/1 | /posts/destroy/colin | Delete
/profile/posts
/profile/posts/new
/profile/posts/1
/profile/posts/1/edit
/profile/posts/1/destroy
resource :user do
resources :devices do
member do
put 'activate'
put 'deactivate'
end
end
end
/user/devices
/user/devices/1
/user/devices/1/edit
/user/devices/1/activate
/user/devices/1/deactivate
# and so forth.
put 'activate', :as => 'enable'
# these are equivalent to one another
resources :devices, :only => [:index, :create, :update, :destroy, :show]
resources :devices, :except => [:new, :edit]
resources :support_requests, :except => :destroy do
member do
delete 'destroy', :action => 'cancel'
end
end
DELETE /support_requests/:id(.:format) #=> {:action=>"cancel", :controller=>"support_requests"}
scope "/admin" do
resources :accounts
end
/admin/accounts
/admin/accounts/new
/admin/accounts/1
# and so forth.
scope :except => [:edit, :new] do
resource :profile
resources :posts
# as many as you want!
end
REST request path | Rails action name | Description
---------------------|-------------------|-------------
GET /posts | index | Render a list of all post
GET /posts/new | new | Render a form for creating a single new post
POST /posts | create | Create a single new post from the received data
GET /posts/1 | show | Render a single existing post
GET /posts/1/edit | edit | Render a form for editing a single existing post
PUT /posts/1 | update | Update a single existing post based on the received data
DELETE /posts/1 | destroy | Destroy a single existing post
/profiles/1/show
REST request path | Rails action name | Description
---------------------|-------------------|-------------
GET /profile/new | new | Render a form for creating the profile
POST /profile | create | Create a the profile from the received data
GET /profile | show | Render a the profile
GET /profile/edit | edit | Render a form for editing the profile
PUT /profile | update | Update the profile based on the received data
DELETE /profile | destroy | Destroy the profile
resource :profile
resources :posts
resource :profile do
resources :posts
end
/profile
/profile/new
/profile/edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment