Skip to content

Part 3 solutions #4

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 6 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
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern

before_action :authenticate_user!
end
22 changes: 12 additions & 10 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class MoviesController < ApplicationController
before_action :set_movie, only: [ :show, :edit, :update, :destroy ]

def new
@movie = Movie.new
end
Expand All @@ -14,12 +16,9 @@ def index
end

def show
@movie = Movie.find(params.fetch(:id))
end

def create
movie_params = params.require(:movie).permit(:title, :description)

@movie = Movie.new(movie_params)

if @movie.valid?
Expand All @@ -32,14 +31,9 @@ def create
end

def edit
@movie = Movie.find(params.fetch(:id))
end

def update
@movie = Movie.find(params.fetch(:id))

movie_params = params.require(:movie).permit(:title, :description)

if @movie.update(movie_params)
redirect_to @movie, notice: "Movie was successfully updated."
else
Expand All @@ -48,10 +42,18 @@ def update
end

def destroy
@movie = Movie.find(params.fetch(:id))

@movie.destroy

redirect_to movies_url, notice: "Movie was successfully destroyed."
end

private

def movie_params
movie_params = params.require(:movie).permit(:title, :description)
end

def set_movie
@movie = Movie.find(params.fetch(:id))
end
end
24 changes: 24 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_users_on_email (email) UNIQUE
# index_users_on_reset_password_token (reset_password_token) UNIQUE
#
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
14 changes: 7 additions & 7 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= render "shared/cdn_assets" %>

<%= yield :head %>

<%# Enable PWA manifest for installable apps (make sure to enable in config/routes.rb too!) %>
Expand All @@ -23,14 +25,12 @@
</head>

<body>
<div style="color: green;">
<%= notice %>
</div>
<%= render "shared/navbar" %>

<div class="container mt-3">
<%= render "shared/flash_messages" %>

<div style="color: red;">
<%= alert %>
<%= yield %>
</div>

<%= yield %>
</body>
</html>
19 changes: 19 additions & 0 deletions app/views/movies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% movie.errors.full_messages.each do |message| %>
<p style="color: red;"><%= message %></p>
<% end %>

<%= form_with(model: movie) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :description %>
<%= form.text_area :description %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
44 changes: 44 additions & 0 deletions app/views/movies/_movie_card.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<div class="card">
<div class="card-header">
<%= link_to "Movie ##{movie.id}", movie %>
</div>

<div class="card-body">
<dl>
<dt>
Title
</dt>
<dd>
<%= movie.title %>
</dd>

<dt>
Description
</dt>
<dd>
<%= movie.description %>
</dd>
</dl>

<div class="row">
<div class="col">
<div class="d-grid">
<%= link_to edit_movie_path(movie), class: "btn btn-outline-secondary" do %>
<i class="fa-regular fa-pen-to-square"></i>
<% end %>
</div>
</div>
<div class="col">
<div class="d-grid">
<%= link_to movie, class: "btn btn-outline-secondary", data: { method: :delete } do %>
<i class="fa-regular fa-trash-can"></i>
<% end %>
</div>
</div>
</div>
</div>

<div class="card-footer">
Last updated <%= time_ago_in_words(movie.updated_at) %> ago
</div>
</div>
20 changes: 1 addition & 19 deletions app/views/movies/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
<h1>Edit movie</h1>

<% @movie.errors.full_messages.each do |message| %>
<p style="color: red;"><%= message %></p>
<% end %>

<%= form_with(model: @movie) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :description %>
<%= form.text_area :description %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
<%= render partial: "movies/form", locals: { movie: @movie } %>
56 changes: 5 additions & 51 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,10 @@

<hr>

<table>
<tr>
<th>
ID
</th>

<th>
Title
</th>

<th>
Description
</th>

<th>
Created at
</th>

<th>
Updated at
</th>

<th>
</th>
</tr>

<div class="row">
<% @movies.each do |movie| %>
<tr>
<td>
<%= movie.id %>
</td>

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

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

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

<td>
<%= link_to "Show details", movie %>
</td>
</tr>
<div class="col-md-3">
<%= render partial: "movies/movie_card", locals: { movie: movie } %>
</div>
<% end %>
</table>
</div>
20 changes: 1 addition & 19 deletions app/views/movies/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
<h1>New movie</h1>

<% @movie.errors.full_messages.each do |message| %>
<p style="color: red;"><%= message %></p>
<% end %>

<%= form_with(model: @movie) do |form| %>
<div>
<%= form.label :title %>
<%= form.text_field :title %>
</div>

<div>
<%= form.label :description %>
<%= form.text_area :description %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
<%= render partial: "movies/form", locals: { movie: @movie } %>
52 changes: 3 additions & 49 deletions app/views/movies/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,51 +1,5 @@
<div>
<div>
<h1>
Movie #<%= @movie.id %> details
</h1>

<div>
<div>
<%= link_to "Go back", movies_path %>
</div>

<div>
<%= link_to "Edit Movie", edit_movie_path(@movie) %>
</div>

<div>
<%= link_to "Delete Movie", @movie, data: { method: :delete } %>
</div>
</div>

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

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

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

<dt>
Updated at
</dt>
<dd>
<%= time_ago_in_words(@movie.updated_at) %> ago
</dd>
</dl>
<div class="row">
<div class="col-md-6 offset-md-3">
<%= render partial: "movies/movie_card", locals: { movie: @movie } %>
</div>
</div>
8 changes: 8 additions & 0 deletions app/views/shared/_cdn_assets.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- Connect Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">

<!-- Connect Bootstrap JavaScript and its dependencies -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

<!-- Connect Font Awesome -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/js/all.min.js"></script>
11 changes: 11 additions & 0 deletions app/views/shared/_flash_messages.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% if notice.present? %>
<div class="alert alert-success" role="alert">
<%= notice %>
</div>
<% end %>

<% if alert.present? %>
<div class="alert alert-danger" role="alert">
<%= alert %>
</div>
<% end %>
30 changes: 30 additions & 0 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<%= link_to "Helper Methods Part 3", root_path, class: "navbar-brand" %>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<%= link_to "Movies", movies_path, class: "nav-link" %>
</li>
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to "#{current_user.email}", edit_user_registration_path(current_user), class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Sign out", destroy_user_session_path, class: "nav-link", data: { method: :delete } %>
</li>
<% else %>
<li class="nav-item">
<%= link_to "Log in", new_user_session_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Sign up", new_user_registration_path, class: "nav-link" %>
</li>
<% end %>
</ul>
</div>
</div>
</nav>
Loading