ActionPack is Controller and View work together to let you interact with resources in the Model layer
REST(Representational State Transfer)
REST is all about resources, You should be to able to:
- List available resources
- Show a specific resource
- Destroy an existing resource
- Provide a way to create a new resource
- Create a new resource
- Provide a way to update an existing resource
- Update an existing resource
REST: A Simple Rails Convention

Rails Convention

Named Routes From
Index
Index action retrieves resources from Data layer. Then, implicitly invokes either HTML or JSON templates.
app/controllers/posts_controller.rb
1 | class PostsController < ApplicationController |
app/views/posts/show.html.erb
1 | <p id="notice"><%= notice %></p> |
- post = post_path(post)
Destroy
和Show
的 named route 都是post
, 因此Destroy
需要指定method: :delete
Show
- Retrieve specific post based on id parameter passed in (as part of URL)
- (Implicit) Look for show.html.erb template to render response
app/controllers/posts_controller.rb
1 | class PostsController < ApplicationController |
app/views/posts/show.html.erb
1 | <p id="notice"><%= notice %></p> |
Delete
destroy action destroys a resource and then redirects
the browser to another page
1 | class PostsController < ApplicationController |
respond_to
- Rails helper that specifies how to respond to a request based on a request format
- Takes an optional block where the argument is the format
- Block specifies how to handle each format:
- format.format_name – matching template
- format.format_name {do_something_other_than_just_displaying_the_ matching_template}
redirect_to
- Instead of rendering a template – send a response to the browser: “Go here!”
New and Create
- New
- Create a new empty post object
- (Implicit) Look for new.html.erb
1 | class PostsController < ApplicationController |
1 | <h1>New Post</h1> |
- Create
- Create a new post object with parameters that were passed from the new form
- Try to save the object to the database
- If successful, redirect to show template
- If unsuccessful, render new action (template - again)
1 | class PostsController < ApplicationController |
Strong parameters
With strong parameters, Action Controller parameters are forbidden to be used in Active Model mass assigments until they have been whitelisted.
Flash
a hash where the data you put in persists for exactly ONE request AFTER the current request.
- Two very common attributes are
:notice
(good) and:alert
(bad) - These are so common in fact, that the redirect_to takes a
:notice
or:alert
keys
1 | flash[:attribute] = value |
1 | <% flash.each do |message_type, message| %> |
Edit and Update
- Edit
- Retrieve a post object based on the id provided (as part of the URl)
- (Implicit) Look for edit.html.erb
1 | class PostsController < ApplicationController |
1 | <h1>Editing Post</h1> |
- Update
- Retrieve an existing post using id parameter
- Update post object with (strong) parameters that were
passed from the edit form - Try to (re)save the object to the database
- If successful, redirect to show template
- If unsuccessful, render edit action (template) again
1 | class PostsController < ApplicationController |
html