Added creation of messages
This commit is contained in:
parent
8a7d5b93e3
commit
c5d6293737
1
Gemfile
1
Gemfile
@ -2,6 +2,7 @@ source 'https://rubygems.org'
|
||||
|
||||
gem 'sinatra', '~> 3.0'
|
||||
gem 'sinatra-contrib', '~> 3.0'
|
||||
gem 'sinatra-flash', '~> 0.3.0'
|
||||
gem 'puma', '~> 6.3'
|
||||
|
||||
gem 'sequel', '~> 5.70'
|
||||
|
@ -3,6 +3,7 @@ Sequel.migration do
|
||||
up do
|
||||
create_table(:messages) do
|
||||
primary_key :id
|
||||
String :slug, null: false, unique: true
|
||||
String :body, null: false
|
||||
DateTime :created_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
DateTime :updated_at, default: Sequel::CURRENT_TIMESTAMP
|
||||
|
33
src/routes/message.rb
Normal file
33
src/routes/message.rb
Normal file
@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'securerandom'
|
||||
|
||||
# /message routes
|
||||
class Destructo < Sinatra::Base
|
||||
|
||||
get '/message' do
|
||||
redirect '/message/new'
|
||||
end
|
||||
|
||||
get '/message/new' do
|
||||
erb :'message/new', locals: {
|
||||
title: 'Create new message'
|
||||
}
|
||||
end
|
||||
|
||||
post '/message/new' do
|
||||
# generate a random string for the slug
|
||||
slug = SecureRandom.urlsafe_base64(12)
|
||||
|
||||
# create object in DB
|
||||
msg = Message.create(
|
||||
slug: slug,
|
||||
body: params[:message_body]
|
||||
)
|
||||
|
||||
flash[:success] = "Your message was created successfully! Share this link: https://msg.destructo.com/#{slug}"
|
||||
|
||||
redirect '/'
|
||||
end
|
||||
|
||||
end
|
@ -4,7 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title><%= title %> | Game Data</title>
|
||||
<title><%= title %> | Destructo</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
|
||||
<link rel="stylesheet" href="/css/hallowvale.css">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js" charset="utf-8"></script>
|
||||
@ -14,6 +14,9 @@
|
||||
<!-- main navigation -->
|
||||
<%= erb :'partials/navbar', :locals => locals %>
|
||||
|
||||
<!-- flash messages -->
|
||||
<%= erb :'partials/flash' %>
|
||||
|
||||
<!-- main content -->
|
||||
<div id="main-content" class="container">
|
||||
<%= yield %>
|
||||
|
18
views/message/new.erb
Normal file
18
views/message/new.erb
Normal file
@ -0,0 +1,18 @@
|
||||
<div class="row">
|
||||
<form class="u-full-width" action="/message/new" method="post">
|
||||
<div class="row">
|
||||
<div class="twelve columns">
|
||||
<label>
|
||||
Message body:
|
||||
<textarea class="u-full-width" name="message_body"></textarea>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="twelve columns">
|
||||
<input class="button-primary" type="submit" value="Create message">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
9
views/partials/flash.erb
Normal file
9
views/partials/flash.erb
Normal file
@ -0,0 +1,9 @@
|
||||
<% if flash[:success] %>
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="columns twelve">
|
||||
<p><%= flash[:success] %></p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<% end %>
|
Loading…
Reference in New Issue
Block a user