Most of my js.erb files contains something like this at the bottom:
$('#flash_message').html('<%= escape_javascript(content_tag(:p, flash[:note], :class => 'note')) %>'); $('#flash_message').fadeOut(2000); $('#loading').remove();
I would like to move these lines into a seperate file and then call that file from each of my js.erb files. Is something like that possible?
Best regards. Asbørn Morell
Yes, you may create an
app/views/shared/_flash_messages.js.rjspartial, which you can then render from anywhere (e.g. from otherrjspartials.)My approach in these kinds of applications has been as follows:
for non-AJAX responses that may have a flash:
layouts/application.erb), add e.g.:render :partial => 'shared/flash_messages.html.erb'for AJAX responses that may also need to display a flash message, I added the following
rjscode:rjsresponse (e.g.controller/action.js.rjs), add e.g.:render :partial => 'shared/flash_messages.js.rjs'Where the two partials do the necessary to render the flash, then call
flash.discard(:error)orflash.discard(:notice)as appropriate.Sample
app/views/shared/flash_messages.html.erbfile:Sample
app/views/shared/flash_messages.html.rjsfile: