0
Simple Restfull apps       with inherited resources
REST       IMAGE
rails 3:  Gemfile : gem inherited_resources  bundle installrails 2.3.x:  gem install inherited_resources --version=1.0.6  ...
1. class ProjectController < InheritedResources::Base2. class ProjectController < ApplicationController        inherit_res...
class ProjectController < InheritedResources::Base   def collection     @projects ||= end_of_association_chain.paginate(.....
Respond To #=>      ...      respond_to :xml, :html, :json      respond_to :js, :only => :create      respond_to :iphone, ...
Overwriting actions    class ProjectController < InheritedResources::Base       def destroy         super do |format|     ...
Overwriting actions    class ProjectController < InheritedResources::Base       def create         create!(:notice => "pro...
Overwriting actions. Errors.    class ProjectController < InheritedResources::Base       def update         update! do |fo...
belongs_to   :project,             :finder => :find_by_title!,             :param => :project_title,             :polymorp...
Decent Exposure  class ArticlesController < ApplicationController private    def articles      @articles ||= Article.order...
Decent Exposure  class ArticlesController < ApplicationController    expose(:article)    expose(:articles) { Article.order...
RespondersInheritedResources.flash_keys           = [ :success, :failure ]flash:   actions:     create:        notice: "%{...
Simple restfull app_s
Simple restfull app_s
Upcoming SlideShare
Loading in...5
×

Simple restfull app_s

815

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
815
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Simple restfull app_s"

  1. 1. Simple Restfull apps with inherited resources
  2. 2. REST IMAGE
  3. 3. rails 3: Gemfile : gem inherited_resources bundle installrails 2.3.x: gem install inherited_resources --version=1.0.6 Inherited resources https://github.com/josevalim/inherited_resources.git
  4. 4. 1. class ProjectController < InheritedResources::Base2. class ProjectController < ApplicationController inherit_resources ... end resource #=> @project collection #=> @projects resource_class #=> Projectclass ProjectController < InheritedResources::Base defaults :resource_class => Market, :collection_name => marketsend
  5. 5. class ProjectController < InheritedResources::Base def collection @projects ||= end_of_association_chain.paginate(...) endendclass ProjectController < InheritedResources::Base def resource ... endend
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. belongs_to :project, :finder => :find_by_title!, :param => :project_title, :polymorphic => true, :singleton => true
  11. 11. 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
  12. 12. Decent Exposure class ArticlesController < ApplicationController expose(:article) expose(:articles) { Article.order(:name) } end
  13. 13. RespondersInheritedResources.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."
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×