Skip to content

Commit

Permalink
Starting point for Helper Methods 3
Browse files Browse the repository at this point in the history
  • Loading branch information
raghubetina committed Jan 10, 2023
1 parent a8cb4f5 commit 19b6057
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 131 deletions.
69 changes: 23 additions & 46 deletions app/controllers/movies_controller.rb
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
30 changes: 10 additions & 20 deletions app/views/movies/edit.html.erb
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 %>
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 a 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 |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
28 changes: 10 additions & 18 deletions app/views/movies/new.html.erb
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 %>
22 changes: 8 additions & 14 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<div>
<div>
<h1>
Movie #<%= @the_movie.id %> details
Movie #<%= @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>

Expand All @@ -29,28 +23,28 @@
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
23 changes: 3 additions & 20 deletions config/routes.rb
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
6 changes: 3 additions & 3 deletions test/system/example_specs.rb → test/system/movie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MoviesTest < ApplicationSystemTestCase

fill_in "Description", with: "A new movie's descroption"
fill_in "Title", with: "A new movie's title"
click_on "Create movie"
click_on "Create Movie"

assert_text "Movie created successfully"

Expand All @@ -30,7 +30,7 @@ class MoviesTest < ApplicationSystemTestCase
fill_in "Description", with: "Some other description"
fill_in "Title", with: "Some other title"

click_on "Update movie"
click_on "Update Movie"

assert_text "Movie updated successfully"

Expand All @@ -45,7 +45,7 @@ class MoviesTest < ApplicationSystemTestCase

visit "/movies"
click_on "Show details", match: :first
click_on "Delete movie"
click_on "Delete Movie"

assert_text "Movie deleted successfully"

Expand Down

0 comments on commit 19b6057

Please sign in to comment.