Skip to content

Instantly share code, notes, and snippets.

@Dylan0203
Forked from dhoelzgen/base_controller.rb
Created August 23, 2017 03:28
Show Gist options
  • Select an option

  • Save Dylan0203/9e1995d37353fe8764cda21d2bb08cb3 to your computer and use it in GitHub Desktop.

Select an option

Save Dylan0203/9e1995d37353fe8764cda21d2bb08cb3 to your computer and use it in GitHub Desktop.

Revisions

  1. @dhoelzgen dhoelzgen revised this gist Jul 14, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion base_controller.rb
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ def cors_set_access_control_headers
    end

    def cors_preflight_check
    if request.method == :options
    if request.method == 'OPTIONS'
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
  2. @dhoelzgen dhoelzgen revised this gist Jul 10, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions base_controller.rb
    Original file line number Diff line number Diff line change
    @@ -7,15 +7,15 @@ class API::V1::BaseController < ApplicationController

    def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = "1728000"
    end

    def cors_preflight_check
    if request.method == :options
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
    headers['Access-Control-Max-Age'] = '1728000'

  3. @dhoelzgen dhoelzgen revised this gist Jul 10, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions whatever_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    class API::V1::WhateverController < API::V1::BaseController

    def upload
    # Do complicated super secret stuff

    render json: { success: true }
    end

    end
  4. @dhoelzgen dhoelzgen revised this gist Jul 10, 2014. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions routes.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Rails.application.routes.draw do
    namespace :api, :defaults => {:format => :json} do
    namespace :v1 do

    controller :whatever, path: '/whatever' do
    match 'post_action', via: [ :post, :options]
    end

    end
    end
    end
  5. @dhoelzgen dhoelzgen created this gist Jul 10, 2014.
    26 changes: 26 additions & 0 deletions base_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    class API::V1::BaseController < ApplicationController

    skip_before_filter :verify_authenticity_token

    before_filter :cors_preflight_check
    after_filter :cors_set_access_control_headers

    def cors_set_access_control_headers
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token'
    headers['Access-Control-Max-Age'] = "1728000"
    end

    def cors_preflight_check
    if request.method == :options
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, Token'
    headers['Access-Control-Max-Age'] = '1728000'

    render :text => '', :content_type => 'text/plain'
    end
    end

    end