generated from appdev-projects/helper-methods-v1
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8cb4f5
commit 19b6057
Showing
7 changed files
with
65 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,57 @@ | ||
class MoviesController < ApplicationController | ||
def new | ||
@the_movie = Movie.new | ||
|
||
render template: "movies/new.html.erb" | ||
@movie = Movie.new | ||
end | ||
|
||
def index | ||
matching_movies = Movie.all | ||
|
||
@list_of_movies = matching_movies.order({ :created_at => :desc }) | ||
@movies = Movie.order(created_at: :desc) | ||
|
||
respond_to do |format| | ||
format.json do | ||
render json: @list_of_movies | ||
end | ||
format.json { render json: @movies } | ||
|
||
format.html do | ||
render({ :template => "movies/index.html.erb" }) | ||
end | ||
format.html | ||
end | ||
end | ||
|
||
def show | ||
the_id = params.fetch(:id) | ||
|
||
matching_movies = Movie.where({ :id => the_id }) | ||
|
||
@the_movie = matching_movies.first | ||
|
||
render({ :template => "movies/show.html.erb" }) | ||
@movie = Movie.find(params.fetch(:id)) | ||
end | ||
|
||
def create | ||
@the_movie = Movie.new | ||
@the_movie.title = params.fetch("query_title") | ||
@the_movie.description = params.fetch("query_description") | ||
movie_params = params.require(:movie).permit(:title, :description) | ||
|
||
@movie = Movie.new(movie_params) | ||
|
||
if @the_movie.valid? | ||
@the_movie.save | ||
redirect_to("/movies", { :notice => "Movie created successfully." }) | ||
if @movie.valid? | ||
@movie.save | ||
|
||
redirect_to movies_url, notice: "Movie created successfully." | ||
else | ||
render template: "movies/new.html.erb" | ||
render "new" | ||
end | ||
end | ||
|
||
def edit | ||
the_id = params.fetch(:id) | ||
|
||
matching_movies = Movie.where({ :id => the_id }) | ||
|
||
@the_movie = matching_movies.first | ||
|
||
render({ :template => "movies/edit.html.erb" }) | ||
@movie = Movie.find(params.fetch(:id)) | ||
end | ||
|
||
def update | ||
the_id = params.fetch(:id) | ||
the_movie = Movie.where({ :id => the_id }).first | ||
|
||
the_movie.title = params.fetch("query_title") | ||
the_movie.description = params.fetch("query_description") | ||
@movie = Movie.find(params.fetch(:id)) | ||
|
||
if the_movie.valid? | ||
the_movie.save | ||
redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully."} ) | ||
movie_params = params.require(:movie).permit(:title, :description) | ||
|
||
if @movie.update(movie_params) | ||
redirect_to @movie, notice: "Movie updated successfully." | ||
else | ||
redirect_to("/movies/#{the_movie.id}", { :alert => "Movie failed to update successfully." }) | ||
render "edit" | ||
end | ||
end | ||
|
||
def destroy | ||
the_id = params.fetch(:id) | ||
the_movie = Movie.where({ :id => the_id }).first | ||
@movie = Movie.find(params.fetch(:id)) | ||
|
||
the_movie.destroy | ||
@movie.destroy | ||
|
||
redirect_to("/movies", { :notice => "Movie deleted successfully."} ) | ||
redirect_to movies_url, notice: "Movie deleted successfully." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,21 @@ | ||
<h1>Edit movie</h1> | ||
|
||
<% @the_movie.errors.full_messages.each do |message| %> | ||
<% @movie.errors.full_messages.each do |message| %> | ||
<p style="color: red;"><%= message %></p> | ||
<% end %> | ||
|
||
<form action="/movies/<%= @the_movie.id %>" method="post"> | ||
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden"> | ||
|
||
<input name="_method" value="patch" type="hidden"> | ||
|
||
<%= form_with model: @movie do |form| %> | ||
<div> | ||
<label for="title_box"> | ||
Title | ||
</label> | ||
|
||
<input type="text" id="title_box" name="query_title" value="<%= @the_movie.title %>"> | ||
<%= form.label :title %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div> | ||
<label for="description_box"> | ||
Description | ||
</label> | ||
|
||
<textarea id="description_box" name="query_description" rows="3"><%= @the_movie.description %></textarea> | ||
<%= form.label :description %> | ||
<%= form.text_area :description %> | ||
</div> | ||
|
||
<button> | ||
Update movie | ||
</button> | ||
</form> | ||
<div> | ||
<%= form.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
<h1>New movie</h1> | ||
|
||
<% @the_movie.errors.full_messages.each do |message| %> | ||
<% @movie.errors.full_messages.each do |message| %> | ||
<p style="color: red;"><%= message %></p> | ||
<% end %> | ||
|
||
<form action="/movies" method="post"> | ||
<input name="authenticity_token" value="<%= form_authenticity_token %>" type="hidden"> | ||
|
||
<%= form_with model: @movie do |form| %> | ||
<div> | ||
<label for="title_box"> | ||
Title | ||
</label> | ||
|
||
<input type="text" id="title_box" name="query_title" value="<%= @the_movie.title %>"> | ||
<%= form.label :title %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div> | ||
<label for="description_box"> | ||
Description | ||
</label> | ||
|
||
<textarea id="description_box" name="query_description" rows="3"><%= @the_movie.description %></textarea> | ||
<%= form.label :description %> | ||
<%= form.text_area :description %> | ||
</div> | ||
|
||
<button> | ||
Create movie | ||
</button> | ||
</form> | ||
<div> | ||
<%= form.submit %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,5 @@ | ||
Rails.application.routes.draw do | ||
get("/", { :controller => "movies", :action => "index" }) | ||
|
||
# Routes for the Movie resource: | ||
|
||
# CREATE | ||
post("/movies", { :controller => "movies", :action => "create" }) | ||
get("/movies/new", { :controller => "movies", :action => "new" }) | ||
|
||
# READ | ||
get("/movies", { :controller => "movies", :action => "index" }) | ||
get("/movies/:id", { :controller => "movies", :action => "show" }) | ||
root "movies#index" | ||
|
||
# UPDATE | ||
patch("/movies/:id", { :controller => "movies", :action => "update" }) | ||
get("/movies/:id/edit", { :controller => "movies", :action => "edit" }) | ||
|
||
# DELETE | ||
delete("/movies/:id", { :controller => "movies", :action => "destroy" }) | ||
|
||
#------------------------------ | ||
end | ||
resources :movies | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters