SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Simple Restfull app's
       with inherited resources
REST



       IMAGE
rails 3:
  Gemfile : gem 'inherited_resources'
  bundle install
rails 2.3.x:
  gem install inherited_resources --version=1.0.6




                Inherited resources



                https://github.com/josevalim/inherited_resources.git
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
class ProjectController < InheritedResources::Base
   def collection
     @projects ||= end_of_association_chain.paginate(...)
   end
end


class ProjectController < InheritedResources::Base
   def resource
     ...
   end
end
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
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
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
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
belongs_to   :project,

             :finder => :find_by_title!,

             :param => :project_title,

             :polymorphic => true,

             :singleton => true
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
Decent Exposure




  class ArticlesController < ApplicationController

    expose(:article)
    expose(:articles) { Article.order(:name) }

  end
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."

Contenu connexe

Tendances

Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Vikas Chauhan
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX PerformanceScott Wesley
 
Laravel for Web Artisans
Laravel for Web ArtisansLaravel for Web Artisans
Laravel for Web ArtisansRaf Kewl
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwanTse-Ching Ho
 
Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.SWAAM Tech
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Elena Kolevska
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel PassportMichael Peacock
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsNeil Crookes
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5Bukhori Aqid
 
Learn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLearn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLarry Cai
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialKaty Slemon
 
Application Diagnosis with Zend Server Tracing
Application Diagnosis with Zend Server TracingApplication Diagnosis with Zend Server Tracing
Application Diagnosis with Zend Server TracingZendCon
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHPAndru Weir
 

Tendances (20)

Devise and Rails
Devise and RailsDevise and Rails
Devise and Rails
 
Pundit
PunditPundit
Pundit
 
REST API for your WP7 App
REST API for your WP7 AppREST API for your WP7 App
REST API for your WP7 App
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
 
Oracle APEX Performance
Oracle APEX PerformanceOracle APEX Performance
Oracle APEX Performance
 
Laravel for Web Artisans
Laravel for Web ArtisansLaravel for Web Artisans
Laravel for Web Artisans
 
Redmine Betabeers SVQ
Redmine Betabeers SVQRedmine Betabeers SVQ
Redmine Betabeers SVQ
 
devise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwandevise tutorial - 2011 rubyconf taiwan
devise tutorial - 2011 rubyconf taiwan
 
Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.Laravel - Website Development in Php Framework.
Laravel - Website Development in Php Framework.
 
Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5Bootstrat REST APIs with Laravel 5
Bootstrat REST APIs with Laravel 5
 
An introduction to Laravel Passport
An introduction to Laravel PassportAn introduction to Laravel Passport
An introduction to Laravel Passport
 
Designing CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIsDesigning CakePHP plugins for consuming APIs
Designing CakePHP plugins for consuming APIs
 
Getting to know Laravel 5
Getting to know Laravel 5Getting to know Laravel 5
Getting to know Laravel 5
 
Flask
FlaskFlask
Flask
 
CakePHP REST Plugin
CakePHP REST PluginCakePHP REST Plugin
CakePHP REST Plugin
 
Learn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutesLearn Dashing Widget in 90 minutes
Learn Dashing Widget in 90 minutes
 
Task scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorialTask scheduling in laravel 8 tutorial
Task scheduling in laravel 8 tutorial
 
Application Diagnosis with Zend Server Tracing
Application Diagnosis with Zend Server TracingApplication Diagnosis with Zend Server Tracing
Application Diagnosis with Zend Server Tracing
 
19.imagini in laravel5
19.imagini in laravel519.imagini in laravel5
19.imagini in laravel5
 
RESTful Web Development with CakePHP
RESTful Web Development with CakePHPRESTful Web Development with CakePHP
RESTful Web Development with CakePHP
 

En vedette

Four New Categories of EMSI Data
Four New Categories of EMSI DataFour New Categories of EMSI Data
Four New Categories of EMSI DataEMSI
 
2011 1-11 txt web push and response
2011 1-11 txt web push and response2011 1-11 txt web push and response
2011 1-11 txt web push and responseNarayan Srinivasan
 
Segundo a-first term
Segundo a-first termSegundo a-first term
Segundo a-first termladymandona
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

En vedette (9)

La amistad
La amistadLa amistad
La amistad
 
txt web overview
 txt web overview txt web overview
txt web overview
 
La amistad
La amistadLa amistad
La amistad
 
Four New Categories of EMSI Data
Four New Categories of EMSI DataFour New Categories of EMSI Data
Four New Categories of EMSI Data
 
2011 1-11 txt web push and response
2011 1-11 txt web push and response2011 1-11 txt web push and response
2011 1-11 txt web push and response
 
2011 1-11 txt web overview
2011 1-11 txt web overview2011 1-11 txt web overview
2011 1-11 txt web overview
 
Segundo a-first term
Segundo a-first termSegundo a-first term
Segundo a-first term
 
La amistad
La amistadLa amistad
La amistad
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Similaire à Simple restfull app_s

Resource and view
Resource and viewResource and view
Resource and viewPapp Laszlo
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Railsrstankov
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Plataformatec
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slidesCao Van An
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weiboshaokun
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à RubyMicrosoft
 
Lightning talk
Lightning talkLightning talk
Lightning talkGil Gomes
 
Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action packrupicon
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - IntroductionVagmi Mudumbai
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30fiyuer
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2pyjonromero
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Yasuko Ohba
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatternsChul Ju Hong
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-publicChul Ju Hong
 
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxWen-Tien Chang
 

Similaire à Simple restfull app_s (20)

Resource and view
Resource and viewResource and view
Resource and view
 
Ruby/Rails
Ruby/RailsRuby/Rails
Ruby/Rails
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010Código Saudável => Programador Feliz - Rs on Rails 2010
Código Saudável => Programador Feliz - Rs on Rails 2010
 
Rails best practices_slides
Rails best practices_slidesRails best practices_slides
Rails best practices_slides
 
Why ruby
Why rubyWhy ruby
Why ruby
 
The Rails Way
The Rails WayThe Rails Way
The Rails Way
 
More to RoC weibo
More to RoC weiboMore to RoC weibo
More to RoC weibo
 
Introduction à Ruby
Introduction à RubyIntroduction à Ruby
Introduction à Ruby
 
Lightning talk
Lightning talkLightning talk
Lightning talk
 
Rupicon 2014 Action pack
Rupicon 2014 Action packRupicon 2014 Action pack
Rupicon 2014 Action pack
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30How to disassemble one monster app into an ecosystem of 30
How to disassemble one monster app into an ecosystem of 30
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Rails vs Web2py
Rails vs Web2pyRails vs Web2py
Rails vs Web2py
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子Ruby on Rails ステップアップ講座 - 大場寧子
Ruby on Rails ステップアップ講座 - 大場寧子
 
Rails antipatterns
Rails antipatternsRails antipatterns
Rails antipatterns
 
Rails antipattern-public
Rails antipattern-publicRails antipattern-public
Rails antipattern-public
 
Ruby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 AjaxRuby on Rails : RESTful 和 Ajax
Ruby on Rails : RESTful 和 Ajax
 

Simple restfull app_s

  • 1. Simple Restfull app's with inherited resources
  • 2. REST IMAGE
  • 3. rails 3: Gemfile : gem 'inherited_resources' bundle install rails 2.3.x: gem install inherited_resources --version=1.0.6 Inherited resources https://github.com/josevalim/inherited_resources.git
  • 4.
  • 5.
  • 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
  • 12. belongs_to :project, :finder => :find_by_title!, :param => :project_title, :polymorphic => true, :singleton => true
  • 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."