Skip to content

All Routes / Links Cleaned for Submission #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 20 additions & 46 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,54 @@
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." })
redirect_to movies_url, notice: "Created movie."
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
@movie = Movie.find(params.fetch(:id))
movie_params = params.require(:movie).permit(:title, :description)

the_movie.title = params.fetch("query_title")
the_movie.description = params.fetch("query_description")

if the_movie.valid?
the_movie.save
redirect_to("/movies/#{the_movie.id}", { :notice => "Movie updated successfully."} )
if @movie.update(movie_params)
redirect_to @movie, notice: "Updated movie."
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

the_movie.destroy

redirect_to("/movies", { :notice => "Movie deleted successfully."} )
@movie = Movie.find(params.fetch(:id))
@movie.destroy

redirect_to movies_url, notice: "Deleted movie."
end
end
30 changes: 11 additions & 19 deletions app/views/movies/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
<h1>Edit movie</h1>
<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>
<%= form.label :description %>
<%= form.text_field :description %>
</div>

<textarea id="description_box" name="query_description" rows="3"><%= @the_movie.description %></textarea>
<div>
<%= form.submit %>
</div>

<button>
Update Movie
</button>
</form>
<% end %>
18 changes: 8 additions & 10 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<hr>

<div>
<a href="/movies/new">Add a new movie</a>
<%= link_to "Add New Movie", new_movie_path %>
</div>

<hr>
Expand Down Expand Up @@ -36,31 +36,29 @@
</th>
</tr>

<% @list_of_movies.each do |a_movie| %>
<% @movies.each do |a_movie| %>
<tr>
<td>
<%= a_movie.id %>
<%= movie.id %>
</td>

<td>
<%= a_movie.title %>
<%= movie.title %>
</td>

<td>
<%= a_movie.description %>
<%= movie.description %>
</td>

<td>
<%= time_ago_in_words(a_movie.created_at) %> ago
<%= time_ago_in_words(movie.created_at) %> ago
</td>
<td>
<%= time_ago_in_words(a_movie.updated_at) %> ago
<%= time_ago_in_words(movie.updated_at) %> ago
</td>

<td>
<a href="/movies/<%= a_movie.id %>">
Show details
</a>
<%= link_to 'Show Details', movie %>
</td>
</tr>
<% end %>
Expand Down
24 changes: 9 additions & 15 deletions app/views/movies/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
<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>
<%= form.label :description %>
<%= form.text_field :description %>
</div>

<textarea id="description_box" name="query_description" rows="3"><%= @the_movie.description %></textarea>
<div>
<%= form.submit %>
</div>

<button>
Create Movie
</button>
</form>
<% end %>
24 changes: 10 additions & 14 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
<div>
<div>
<h1>
Movie #<%= @the_movie.id %> details
Movie #<%= @the_movie.id %> Details
</h1>

<div>

<div>
<a href="/movies">
Go back
</a>
<%= link_to "Go Back", movies_path %>
</div>

<div>
<a href="/movies/<%= @the_movie.id %>/edit">
Edit Movie
</a>
<%= link_to "Edit Movie", edit_movie_path(@movie) %>
</div>

<div>
<a href="/movies/<%= @the_movie.id %>" data-method="delete">
Delete Movie
</a>
<%= link_to "Delete Movie", @movie, method: :delete %>
</div>

</div>

<dl>
<dt>
Title
</dt>
<dd>
<%= @the_movie.title %>
<%= @movie.title %>
</dd>

<dt>
Description
</dt>
<dd>
<%= @the_movie.description %>
<%= @movie.description %>
</dd>

<dt>
Created at
</dt>
<dd>
<%= time_ago_in_words(@the_movie.created_at) %> ago
<%= time_ago_in_words(@movie.created_at) %> ago
</dd>

<dt>
Updated at
</dt>
<dd>
<%= time_ago_in_words(@the_movie.updated_at) %> ago
<%= time_ago_in_words(@movie.updated_at) %> ago
</dd>
</dl>
</div>
Expand Down
20 changes: 3 additions & 17 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
Rails.application.routes.draw do
get("/", { :controller => "movies", :action => "index" })
root "movies#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" })

# UPDATE
patch("/movies/:id", { :controller => "movies", :action => "update" })
get("/movies/:id/edit", { :controller => "movies", :action => "edit" })

# DELETE
delete("/movies/:id", { :controller => "movies", :action => "destroy" })
resources :movies

#------------------------------
end
end