From b61dadb7905e3dbde44750f5784c1f4a06f7ca27 Mon Sep 17 00:00:00 2001 From: Monick Date: Wed, 18 Dec 2019 10:55:09 -0800 Subject: [PATCH 1/2] added create route and action --- app/controllers/movies_controller.rb | 12 ++++++++++++ config/routes.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..1c73e339 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,18 @@ def show ) end + def create + new_movie = Movie.new(params) + + if new_movie.save + render json: new_movie.as_json(only: [:id]), status: :created + return + else + render json: {ok: false, errors: new_movie.errors.messages}, status: :bad_request + return + end + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 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" From ba6f8c9cbdc457b093edf14e9cc638bb0c7b19e6 Mon Sep 17 00:00:00 2001 From: Monick Date: Wed, 18 Dec 2019 11:39:53 -0800 Subject: [PATCH 2/2] added movie params --- app/controllers/movies_controller.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 1c73e339..70f478dd 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,7 +22,7 @@ def show end def create - new_movie = Movie.new(params) + new_movie = Movie.new(movie_params) if new_movie.save render json: new_movie.as_json(only: [:id]), status: :created @@ -41,4 +41,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.permit(:title, :release_date, :overview, :inventory) + end end