Skip to content
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
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
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 updated successfully."
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 deleted successfully."
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
26 changes: 26 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# == Schema Information
#
# Table name: users
#
# id :bigint not null, primary key
# email :string default(""), not null
# encrypted_password :string default(""), not null
# first_name :string
# last_name :string
# 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 @@ -6,19 +6,19 @@
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= render "shared/cdn_assets" %>

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_importmap_tags %>
</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>
15 changes: 15 additions & 0 deletions app/views/movies/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= form_with(model: foo, data: { turbo: false }) 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 ##{baz.id}", baz %>
</div>

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

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

<div class="row">
<div class="col">
<div class="d-grid">
<%= link_to edit_movie_path(baz), 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 baz, class: "btn btn-outline-secondary", data: { turbo_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(baz.updated_at) %> ago
</div>
</div>
16 changes: 1 addition & 15 deletions app/views/movies/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,4 @@
<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: { foo: @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: { baz: movie } %>
</div>
<% end %>
</table>
</div>
16 changes: 1 addition & 15 deletions app/views/movies/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,4 @@
<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: { foo: @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, 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: { baz: @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/bootstrap@5.2.3/dist/css/bootstrap.min.css">

<!-- Connect Bootstrap JavaScript and its dependencies -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/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.first_name} #{current_user.last_name}", 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: { turbo_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>
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require "active_support/core_ext/integer/time"

Rails.application.configure do
# Devise action mailer setup
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

# Allow server to be hosted on any URL
config.hosts.clear
# Allow better_errors to work in online IDE
Expand Down
Loading