I'm trying to follow the Ember Getting Started guide and build a TodoMVC app but use Ember-CLI with Rails as a backend. Unfortunately I'm running into an issue with cross site domain stuff. I'm getting this error message with I try and do a post request:
XMLHttpRequest cannot load http://localhost:3000/api/todos. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
On the Rails side I have Rack Cors installed. I have it added to my Gemfile:
gem 'rack-cors', :require => 'rack/cors'
And in my application.rb
file I have:
module Todoemberrails
class Application < Rails::Application
config.assets.enabled = false
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: [:get, :post, :put, :delete, :options]
end
end
end
end
And this is my controller:
class Api::TodosController < ApplicationController
def index
render json: Todo.all
end
def show
render json: Todo.find(params[:id])
end
def create
todo = Todo.new(todo_params)
if todo.save
render json: todo, status: :created
else
render json: todo.errors, status: :unprocessed
end
end
private
def todo_params
params.require(:todo).permit(:title, :is_completed)
end
end
And inside of my Ember app in app/adapters/application.js
I have:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
host: 'http://localhost:3000/api'
});