Browse Source

flash message via redirect

main
pvincent 1 week ago
parent
commit
294771a5de
  1. 9
      app/controllers/notification_controller.rb
  2. 3
      app/views/layouts/application.html.erb
  3. 16
      app/views/layouts/components/_flash.html.erb
  4. 9
      app/views/notification/index.html.erb
  5. 15
      config/routes.rb

9
app/controllers/notification_controller.rb

@ -1,4 +1,13 @@
class NotificationController < ApplicationController
def index
logger.info 'index'
end
def show_notice
redirect_to notification_index_path, notice: 'this is a notice'
end
def show_alert
redirect_to notification_index_path, alert: 'this is an alert'
end
end

3
app/views/layouts/application.html.erb

@ -23,7 +23,8 @@
</head>
<body class='dark:bg-gray-800 dark:text-slate-500'>
<main class="container mx-auto mt-28 px-5 flex">
<%= render 'layouts/components/flash'%>
<main class="container mx-auto mt-28 px-5 flex-col gap-4">
<%= yield %>
</main>
</body>

16
app/views/layouts/components/_flash.html.erb

@ -0,0 +1,16 @@
<% if flash.any? %>
<h2 class='bold text-2xl'>FLASH</h2>
<ul>
<li>
<% if flash.key?(:alert) %>
ALERT = <%= flash.alert %>
<% end %>
</li>
<li>
<% if flash.key?(:notice) %>
NOTICE = <%= flash.notice %>
<% end %>
</li>
</ul>
<% end %>

9
app/views/notification/index.html.erb

@ -1,4 +1,11 @@
<div>
<h1 class="font-bold text-4xl">Notification#index</h1>
<p>Find me in app/views/notification/index.html.erb</p>
</div>
<div>
<h2 class="text-bold text-2xl">Links</h2>
<ul>
<li><%= link_to 'Notice', notification_show_notice_path%></li>
<li><%= link_to 'Alert', notification_show_alert_path%></li>
<li><%= link_to 'Nothing', notification_index_path%></li>
</ul>

15
config/routes.rb

@ -1,15 +1,6 @@
Rails.application.routes.draw do
root to: 'notification#index'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
# Defines the root path route ("/")
# root "posts#index"
get 'notification/index'
get 'notification/show_notice'
get 'notification/show_alert'
end
Loading…
Cancel
Save