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 existpull/18/head
parent
c7fee27623
commit
5bce31baf2
11
app.rb
11
app.rb
|
|
@ -19,11 +19,10 @@ class App < Roda
|
||||||
|
|
||||||
r.get String do | url_code |
|
r.get String do | url_code |
|
||||||
link = links.filter(:code => url_code)
|
link = links.filter(:code => url_code)
|
||||||
if link.first.nil?
|
r.redirect link.first[:url] unless link.first.nil?
|
||||||
flash['message'] = "Link #{url_code} doesn't exist"
|
@message = "Link #{url_code} doesn't exist"
|
||||||
r.redirect '/'
|
response.status = 404
|
||||||
end
|
view :home
|
||||||
r.redirect link.first[:url]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
r.post "create" do
|
r.post "create" do
|
||||||
|
|
@ -32,7 +31,7 @@ class App < Roda
|
||||||
flash['message'] = "Please enter a valid URL";
|
flash['message'] = "Please enter a valid URL";
|
||||||
r.redirect '/'
|
r.redirect '/'
|
||||||
end
|
end
|
||||||
if nil == links.filter(:url => url).first
|
if links.filter(:url => url).first.nil?
|
||||||
code = SecureRandom.urlsafe_base64 4
|
code = SecureRandom.urlsafe_base64 4
|
||||||
links.insert(url: url, code: code)
|
links.insert(url: url, code: code)
|
||||||
@message = "Link created"
|
@message = "Link created"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue