Saturday 20 December 2014

REST and RESTful Routes in Rails


What is REST?
  •   Representational State Transfer
  •   Don't perform procedures
  •   Perform sate transformations upon resources

Why do we need use REST?
  •  Default routes (using hashes) works
  •  REST is Rails default; optimized for it
  •  Most professional Rails developers use REST
  •  Gives an application an API for free(HTML,XML,JSON etc.,)
  •  Provides conventions,consistent structure, and simplicity
  •  Improves application security

REST Paradigm Requirements

  1.  Organize code into resources - Rails encourages one controller for each model
  2.  Learn HTTP verbs for working with resources - Consider which CRUD  actions are suited for each
  3.  Map a new URL syntax to controller actions - Change Rails routes
  4.  Modify existing links and forms to use new URL syntax - Learn about RESTful route helpers
   














REST in Rails

    <form method="PATCH" action="/subjects/1243">
    #......
    </form>

    <form method="POST" action="/subjects/1243">
    <input type="hidden" name="_method" value="patch"/>
    #......
    </form>

RESTful Routes:

    # config/routes.rb

    resources :subjects do
      member do
        get  :delete
          end
        end














   To know routes in your application: rake routes


RESTful Links and Forms

    RESTful URL Helpers
   
        {:controller => 'subjects', :action => 'show', :id =>5} 
   
         subject_path(5)
  


    <%= link_to ('All Subjects', subjects_path) %>
    <%= link_to ('Show Subject', subject_path(@subject.id)) %>
    <%= link_to ('Edit Subject', edit_subject_path(@subject.id)) %>

    for additional arguments use hash
    <%= link_to ('Edit Subject',
      edit_subject_path(@subject.id, :page => 1, :sort => 'name')) %>

    REST Form Helpers

    form_for(:subject, :url => subject_path(@subject.id),
      :html => {:method => :patch}) do
      #...........
    end

    form_for(@subject) do |f|
     #.....
     end

Using Non-Standard Resources

    resources :admin_users, :except => [:show]
    resources :products,    :only => [:index, :show]

Using Non-Standard Actions

    resources :subjects do
   
      member do
        get :delete     # delete_subject_path(:id)
      end

      collection do
        get :export        # export_subjects_path
      end
    end

Nested Resources

    resources :subjects do
     member do
        get :delete
      end
     
       resources :pages do
         member do
          get :delete
          end
       end

    end




  









  
REST URL Helpers
   
    <%= link_to('All Pages', subject_pages_path(@subject.id)) %>
    <%= link_to('Show Page', subject_page_path(@subject.id, @page.id)) %>
    <%= link_to('Edit Page',
        edit_subject_page_path(@subject.id, @page.id)) %>
    <%= link_to('All Sections',
        subject_page_sections_path(@subject.id, @page.id)) %>
    <%= link_to('Show Section',
        subject_page_section_path(@subject.id, @page.id,@section.id)) %>

No comments:

Post a Comment