From b40b5dbf4410dd246202ce2e44aaf8834289d973 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Wed, 14 Jun 2017 12:53:52 -0700 Subject: [PATCH 1/2] Fix the CORS issue using the rack-cors gem --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index d5c7affd..d837ba0e 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,8 @@ gem 'jbuilder', '~> 2.5' gem 'will_paginate' +gem 'rack-cors', :require => 'rack/cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..f80bcea9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,7 @@ GEM pry (>= 0.9.10) puma (3.6.2) rack (2.0.1) + rack-cors (0.4.1) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -208,6 +209,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/config/application.rb b/config/application.rb index 2ac21a62..9f8a6f91 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,9 +15,11 @@ class Application < Rails::Application #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') - config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => 'http://localhost:8081', - 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") - } + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', :headers => :any, :methods => [:get, :post, :put, :delete, :options] + end + end end end From c069f4d7c984f8521bbfd7d8acb442e31991695c Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Fri, 16 Jun 2017 10:16:50 -0700 Subject: [PATCH 2/2] added route for create and create function in the movies controller --- app/controllers/movies_controller.rb | 20 +++++++++++++++++++- config/routes.rb | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6e9c8de7..d40907df 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,7 +24,21 @@ def show def create # check if the movie is already in the rails db by checking if there is a movie in the rails db with the same title and release date from params (use find by) # if the movie already exists in the rails db, then increase the inventory by 1 - # set inventory to 1 when we create the movie in the rails db + # set inventory to 1 when we create the movie in the rails db + + # movie = Movie.find_by(title: params[:title]) + # if movie + # movie.inventory += 1 + # else + movie = Movie.new(movie_params) + if movie.save + render status: :ok, json: {id: movie.id} + else + render status: :bad_request, json: { errors: movie.errors.messages } + end + # end + + end @@ -36,4 +50,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.require.permit(:title, :overview, :release_date, :inventory, :image_url) + end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..8eff1ff6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:index, :show, :create], param: :title post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"