Simple restfull app_s
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Simple restfull app_s

on

  • 961 views

 

Statistics

Views

Total Views
961
Views on SlideShare
961
Embed Views
0

Actions

Likes
1
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Simple restfull app_s Presentation Transcript

  • 1. Simple Restfull apps with inherited resources
  • 2. REST IMAGE
  • 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. 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. class ProjectController < InheritedResources::Base def collection @projects ||= end_of_association_chain.paginate(...) endendclass ProjectController < InheritedResources::Base def resource ... endend
  • 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. 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. 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. 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. belongs_to :project, :finder => :find_by_title!, :param => :project_title, :polymorphic => true, :singleton => true
  • 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. Decent Exposure class ArticlesController < ApplicationController expose(:article) expose(:articles) { Article.order(:name) } end
  • 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."