Add reminder feature

This commit is contained in:
2020-02-12 13:57:03 +01:00
parent 9ec7b3ef50
commit e4a317111c
26 changed files with 414 additions and 3 deletions

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Reminders controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@ -0,0 +1,65 @@
body {
background-color: #fff;
color: #333;
margin: 33px; }
body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000; }
a:visited {
color: #666; }
a:hover {
color: #fff;
background-color: #000; }
th {
padding-bottom: 5px; }
td {
padding: 0 5px 7px; }
div.field,
div.actions {
margin-bottom: 10px; }
#notice {
color: green; }
.field_with_errors {
padding: 2px;
background-color: red;
display: table; }
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0; }
#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff; }
#error_explanation ul li {
font-size: 12px;
list-style: square; }
label {
display: block; }

View File

@ -1,2 +1,3 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end

View File

@ -0,0 +1,51 @@
class RemindersController < ApplicationController
before_action :set_reminder, only: %i[show edit update destroy]
def index
@reminders = Reminder.all
end
def show; end
def new
@reminder = Reminder.new
end
def edit; end
def create
@reminder = Reminder.new(reminder_params)
@reminder.user_id = current_user.id
if @reminder.save
redirect_to @reminder, notice: 'Reminder was successfully created.'
else
render :new
end
end
def update
if @reminder.update(reminder_params)
redirect_to @reminder, notice: 'Reminder was successfully updated.'
else
render :edit
end
end
def destroy
@reminder.destroy
redirect_to reminders_url, notice: 'Reminder was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reminder
@reminder = Reminder.find(params[:id])
end
# Only allow a list of trusted parameters through.
def reminder_params
params.require(:reminder).permit(:title, :body, :date, :user_id)
end
end

View File

@ -0,0 +1,2 @@
module RemindersHelper
end

7
app/models/reminder.rb Normal file
View File

@ -0,0 +1,7 @@
class Reminder < ApplicationRecord
belongs_to :user
validates :title, presence: true
validates :body, presence: true
validates :date, presence: true
end

View File

@ -11,5 +11,10 @@
<body>
<%= yield %>
<hr>
<% if user_signed_in? %>
<p><%= link_to('Logout', destroy_user_session_path) %></p>
<% end %>
</body>
</html>

View File

@ -0,0 +1,32 @@
<%= form_with(model: reminder, local: true) do |form| %>
<% if reminder.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(reminder.errors.count, "error") %> prohibited this reminder from being saved:</h2>
<ul>
<% reminder.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :body %>
<%= form.text_area :body %>
</div>
<div class="field">
<%= form.label :date %>
<%= form.datetime_select :date %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing Reminder</h1>
<%= render 'form', reminder: @reminder %>
<%= link_to 'Show', @reminder %> |
<%= link_to 'Back', reminders_path %>

View File

@ -0,0 +1,31 @@
<p id="notice"><%= notice %></p>
<h1>Reminders</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Body</th>
<th>Date</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @reminders.each do |reminder| %>
<tr>
<td><%= reminder.title %></td>
<td><%= reminder.body %></td>
<td><%= reminder.date %></td>
<td><%= link_to 'Show', reminder %></td>
<td><%= link_to 'Edit', edit_reminder_path(reminder) %></td>
<td><%= link_to 'Destroy', reminder, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Reminder', new_reminder_path %>

View File

@ -0,0 +1,5 @@
<h1>New Reminder</h1>
<%= render 'form', reminder: @reminder %>
<%= link_to 'Back', reminders_path %>

View File

@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @reminder.title %>
</p>
<p>
<strong>Body:</strong>
<%= @reminder.body %>
</p>
<p>
<strong>Date:</strong>
<%= @reminder.date %>
</p>
<%= link_to 'Edit', edit_reminder_path(@reminder) %> |
<%= link_to 'Back', reminders_path %>