Sto utilizzando questa libreria, (https://github.com/CodeSeven/toastr) e sto provando a inviare le mie notifiche Flash alla funzione javascript che Toastr ha fornito per me. Come posso chiamare questa funzione per ogni errore o notifica?Rails 4 - Notifiche di toaster anziché notifiche flash
Questo è uno dei metodi che vengono utilizzati per fare una notifica tostapane:
toastr.warning('This is a warning!')
Ho provato a fare un metodo nel ApplicationController per vedere se potevo chiamare tale metodo in caso di errori di default da CanCan. Ho varie versioni del metodo, nessuna delle quali ha funzionato.
def toast(type, text)
#if Logic here for various errors/notifications
respond_to do |format|
format.js { render action: "toastr.warning(#{text})", layout: false}
end
end
def toast(type, text)
#if Logic here for various errors/notifications
"toastr.warning(#{text})"
end
E poi cerco di utilizzare questo metodo nel blocco CanCan:
rescue_from CanCan::AccessDenied do |exception|
toast :error, exception.message
redirect_to root_url
end
presumo che questo è possibile, ma io sono solo sicuro come implementarlo. Non molti cercano di farlo, e probabilmente c'è una ragione. Sono aperto a qualsiasi suggerimento su come fare ciò che sto cercando di fare.
Ecco un'applicazione di test che implementa le notifiche Toast: http://codeseven.github.io/toastr/demo.html
Con un po 'di regolazione come aggiungere 'Raw' su' message.inspect'. L'ho fatto funzionare. Grazie per l'aiuto! – Rizowski