Skip to content

Commit

Permalink
Merge pull request #1 from Mello-Cello/createMovie
Browse files Browse the repository at this point in the history
merge createMovie function in movie controller to master
  • Loading branch information
lebaongoc authored Jun 26, 2019
2 parents a862498 + be51857 commit d7720a9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ def show
)
end

def create

# Rails has set up that everything that comes in from a request as query params... gets into the params object
@movie = Movie.new(movie_params)
puts "we're in the create function ****************************************"
puts params#["apples"]
# How do we make sure that this new instance of movie has actually the values coming from the request ... Hm...(strong params!)
# Traditionally, we have put this functionality of reading from the request and also some cool rails security things using Strong Params

@movie.save

if @movie.save
render json: {movie: {id: @movie.id, title: @movie.title}}, status: :ok
else
render json: {errors: @movie.errors.messages},
status: :bad_request
end

end

private

def require_movie
Expand All @@ -29,4 +49,9 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
return params.require(:movie).permit(:title, :overview, :release_date, :inventory) # :available_inventory ?
end

end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit d7720a9

Please sign in to comment.