Skip to content

Instantly share code, notes, and snippets.

@serv
Created July 19, 2014 07:11
Show Gist options
  • Save serv/441cb713bb55c6fdeb80 to your computer and use it in GitHub Desktop.
Save serv/441cb713bb55c6fdeb80 to your computer and use it in GitHub Desktop.
Example of setting header to support CORS on Rails 4 API server side
module Api
module V0
class LinksController < ApplicationController
# THIS
before_action :set_headers
def show
@link = Link.find(params[:id])
render json: @link
end
private
def link_params
params.require(:status).permit(:title, :href, :comment)
end
# THIS
def set_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment