From 5bce31baf283a051d1213c30601355494939d8d5 Mon Sep 17 00:00:00 2001 From: Adam Townsend Date: Sat, 7 Oct 2023 21:11:12 -0700 Subject: [PATCH] refactored missing link scenario to make more sense + 404 code, stay on the current page, and load the form + also simplified the logic for the create action if the link doesn't already exist --- app.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app.rb b/app.rb index 44dae4c..2e49f5e 100644 --- a/app.rb +++ b/app.rb @@ -19,11 +19,10 @@ class App < Roda r.get String do | url_code | link = links.filter(:code => url_code) - if link.first.nil? - flash['message'] = "Link #{url_code} doesn't exist" - r.redirect '/' - end - r.redirect link.first[:url] + r.redirect link.first[:url] unless link.first.nil? + @message = "Link #{url_code} doesn't exist" + response.status = 404 + view :home end r.post "create" do @@ -32,7 +31,7 @@ class App < Roda flash['message'] = "Please enter a valid URL"; r.redirect '/' end - if nil == links.filter(:url => url).first + if links.filter(:url => url).first.nil? code = SecureRandom.urlsafe_base64 4 links.insert(url: url, code: code) @message = "Link created"