Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Changes regarding HTTP PATCH in Rails 4

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 248
    Comment on it

    Hi Friends,


    There were so many changes came with introduction of Rails. So one the major change was regarding the HTTP Patch Requests. From rails 4, whenever a resource is declared in the routes, by default it uses PATCH as primary verb for updates, but put will continue to work as it is and will route to the update action, so in case if you are using the the standard RESTful resource, like this:

    ## inside config/routes.rb
    
    resources :events
    
    
    ## Inside form
    
    <%= form_for @event do |f| %>
    
    
    ## Inside events_controller.rb
    
    class EventsController < ApplicationController
      def update
        
      end
    end


    In the above case, you don't need to change anything, but if you are using custom routes for updates like in this example:

     

    ## inside config/routes.rb
    
    resources :events, do
      put :update_event_locationt, on: :member
    end
    
    
    ## Inside form
    
    <%= form_for [ :update_event_location, @event ] do |f| %>
    
    
    ## Inside events_controller.rb
    
    class EventsController < ApplicationController
      def update_event_location
        
      end
    end


    In this case, the form will try to find the patch request for update_event_location, which is not available in this case, so it will not work properly, Thus to get rid this of this problem you have two options:

    a) Change the method of the http from put to patch:

    Ideal but may not be helpful in case you are having a public api

     

    ## inside config/routes.rb
    resources :event, do
      patch :update_event_location, on: :member
    end
    

     

    b) Specify the method in your form like this:

     

    
    ## inside your form:
    
    <%= form_for [ :update_event_location, @user ], method: :put do |f| %>


    Hoping it was helpful for you to know.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: