| Path: | DEFAULTS |
| Last Update: | Mon Nov 05 00:01:40 UTC 2007 |
There’s a rough equivalence between a basic @make_resourceful@ call and a hand-made controller. This:
class PostsController < ApplicationController
make_resourceful { actions :all }
end
Creates a controller that works more or less like the one that follows. Note that the real code generated by make_resourceful is more extensible in various ways. Thus whenever possible, there are comments in the following controller indicating how to customize various bits of the controller.
class PostsController < ApplicationController
def index
# Override #current_objects to change this
@posts = Post.find(:all)
# Use before :index to add something here
# Use response_for :index to change this
respond_to { |f| f.html; f.js }
end
def show
# Override #current_object to change this
@post = Post.find(params[:id])
# Use before :show to add something here
# Use response_for :show to change this
respond_to { |f| f.html; f.js }
end
def create
# Override #build_object to change this
@post = Post.new(params[:post])
# Use before :create to add something here
if @post.save
# Use after :create to add something here
# Use response_for :create to change this
respond_to do |f|
f.html do
flash[:notice] = "Create successful!"
redirect_to post_path(@post)
end
f.js
end
else
# Use after :create_fails to add something here
# Use response_for :create_fails to change this
respond_to do |f|
format.html
flash[:error] = "There was a problem!"
render :action => :new, :status => 422
end
format.js
end
end
end
def update
# Override #current_object to change this
@post = Post.find(params[:id])
# Use before :update to do something here
if @post.update_attributes params[:post]
# Use after :update to add something here
# Use response_for :update to change this
respond_to do |f|
f.html do
flash[:notice] = "Save successful!"
redirect_to post_path(@post)
end
f.js
end
else
# Use after :update_fails to add something here
# Use response_for :update_fails to change this
respond_to do |f|
format.html
flash[:error] = "There was a problem saving!"
render :action => :edit, :status => 422
end
format.js
end
end
end
def new
# Override #build_object to change this
@post = Post.new(params[:post])
# Use before :new to add something here
# Use response_for :new to change this
respond_to { |f| f.html; f.js }
end
def edit
# Override #current_object to change this
@post = Post.find(params[:id])
# Use before :edit to add something here
# Use response_for :edit to change this
respond_to { |f| f.html; f.js }
end
def destroy
# Override #current_object to change this
@post = Post.find(params[:id])
# Use before :destroy to do something here
if @post.destroy
# Use after :destroy to add something here
# Use response_for :destroy to change this
respond_to do |f|
f.html do
flash[:notice] = "Record deleted!"
redirect_to posts_path(@post)
end
f.js
end
else
# Use after :destroy_fails to add something here
# Use response_for :destroy_fails to change this
respond_to do |f|
format.html
flash[:error] = "There was a problem deleting!"
render :back
end
format.js
end
end
end
end