6. 1. class ProjectController < InheritedResources::Base
2. class ProjectController < ApplicationController
inherit_resources
...
end
resource #=> @project
collection #=> @projects
resource_class #=> Project
class ProjectController < InheritedResources::Base
defaults :resource_class => Market, :collection_name => 'markets'
end
7. class ProjectController < InheritedResources::Base
def collection
@projects ||= end_of_association_chain.paginate(...)
end
end
class ProjectController < InheritedResources::Base
def resource
...
end
end
8. Respond To #=>
...
respond_to :xml, :html, :json
respond_to :js, :only => :create
respond_to :iphone, :except => [:edit, :update]
...
And actions #=>
class ProjectsController < InheritedResources::Base
actions :index, :show, :create, :update
# or
actions :all, :except => [:update, :destroy]
end
9. Overwriting actions
class ProjectController < InheritedResources::Base
def destroy
super do |format|
format.html { redirect_to root_url }
end
end
end
class ProjectController < InheritedResources::Base
def destroy
destroy!{ root_url }
end
end
10. Overwriting actions
class ProjectController < InheritedResources::Base
def create
create!(:notice => "project created" ) { root_url }
end
end
class ProjectController < InheritedResources::Base
def create
@project = Project.new(params[:project])
@project.something_special
create! # or super
end
end
11. Overwriting actions. Errors.
class ProjectController < InheritedResources::Base
def update
update! do |format|
unless @project.errors.empty?
format.html { projects_url( @project ) }
end
end
end
end
class ProjectController < InheritedResources::Base
def update
update! do |success, failure|
failure.html { projects_url( @project ) }
end
end
end
13. Decent Exposure
class ArticlesController < ApplicationController private
def articles
@articles ||= Article.order(:name)
end
helper_method :articles
def article
@article ||= #some else get or new
end
helper_method :article
end
14. Decent Exposure
class ArticlesController < ApplicationController
expose(:article)
expose(:articles) { Article.order(:name) }
end
15. Responders
InheritedResources.flash_keys = [ :success, :failure ]
flash:
actions:
create:
notice: "%{resource_name} was successfully created."
update:
notice: "%{resource_name} was successfully updated."
destroy:
notice: "%{resource_name} was successfully destroyed."
alert: "%{resource_name} could not be destroyed."