From a5018ba9174f16dfb85db8b5d80ba3440c1739a2 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Mon, 25 Sep 2023 15:43:37 -0500 Subject: [PATCH] Complete Lesson 13 --- app/controllers/movies_controller.rb | 15 ++++++++++++++- app/views/movies/index.html.erb | 3 +++ app/views/movies/new.html.erb | 20 ++++++++++++++++++++ config/routes.rb | 6 ++---- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 app/views/movies/new.html.erb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index d8ce621..d5595db 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,8 +13,21 @@ class MoviesController < ApplicationController end def update @movie = Movie.find(params[:id]) - movie_params = params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross) @movie.update(movie_params) redirect_to @movie end + def new + @movie = Movie.new + end + def create + @movie = Movie.new(movie_params) + @movie.save + redirect_to @movie + end + + private + + def movie_params + params.require(:movie).permit(:title, :description, :rating, :released_on, :total_gross) + end end diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb index 52f31a3..b75de10 100644 --- a/app/views/movies/index.html.erb +++ b/app/views/movies/index.html.erb @@ -17,4 +17,7 @@ <% end %> +
+ <%= link_to "Add New Movie", new_movie_path, class: "button" %> +
diff --git a/app/views/movies/new.html.erb b/app/views/movies/new.html.erb new file mode 100644 index 0000000..56b8d51 --- /dev/null +++ b/app/views/movies/new.html.erb @@ -0,0 +1,20 @@ +

Creating a New Movie

+ +<%= form_with(model: @movie) do |f| %> + <%= f.label :title %> + <%= f.text_field :title %> + + <%= f.label :description %> + <%= f.text_area :description, rows: 7 %> + + <%= f.label :rating %> + <%= f.text_field :rating %> + + <%= f.label :released_on %> + <%= f.date_field :released_on %> + + <%= f.label :total_gross %> + <%= f.number_field :total_gross %> + + <%= f.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 210108c..32474ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,5 @@ Rails.application.routes.draw do root "movies#index" - get "movies" => "movies#index" - get "movies/:id" => "movies#show", as: "movie" - patch "movies/:id" => "movies#update" - get "movies/:id/edit" => "movies#edit", as: "edit_movie" + + resources :movies end