Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Automagically include your flash messages in your Ruby on Rails Hotwire TurboStream responses.

License

NotificationsYou must be signed in to change notification settings

joshmn/turbo_flash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

For the sake of your application, consider usingHotFlash instead.

TurboFlash

Automagically include your flash messages in your Ruby on RailsTurboStream responses.

Video demo

Usage

By default, TurboFlash will inherit all flashes that you normally set. This can be turned off with theinherit_flashesconfiguration flag.

To explicitly set flashes, TurboFlash exposes aflash.turbo method that's similar toflash:

classUsersController <ApplicationControllerdefupdate@user=current_userunless@user.valid_password?(user_params[:current_password])@user.errors.add(:current_password,"is invalid.")returnrespond_todo |f|f.htmldoflash.now[:notice]="There was an error."render:showendf.turbo_streamdoflash.turbo[:notice]="There was an error."renderturbo_stream:turbo_stream.replace(@user,partial:'form')endendend# ...endprivatedefuser_paramsparams.require(:user).permit(:current_password,:password,:password_confirmation)endend

Of course, the response will be what you expect:

turbo_stream.replace(@user,partial:'form')

TurboFlash will also automatically inject the following into your Turbo response:

turbo_stream.update("flash",partial:"shared/flash",locals:{role::notice,message:"There was an error."})

If you want to get more granular, useflash.turbo#set_options:

flash.turbo.set_options(action::append)[:notice]="This will be appended."

You could even:

flash.turbo.set_options(action::append,partial:"layouts/_cool_awesome_flash")[:error]="This will be appended from the partial cool_awesome_flash with an error role."

The defaults are customizable and shown below.

Installation

Add this line to your application's Gemfile:

gem'turbo_flash'

And then execute:

$ bundle

Create a partial ofshared/_flash.html.erb:

<divclass="flash flash-<%=local_assigns[:role]%>"><%=local_assigns[:message]%></div>

Ensure that the TurboStream target — a tag with anid offlash exists in your layout:

<!DOCTYPE html><html><head><%=csrf_meta_tags%><%=csp_meta_tag%><%=stylesheet_link_tag'application',media:'all',data:{turbo_track::reload}%><%=javascript_pack_tag'application',data:{turbo_track::reload}%></head><body><divid="flash"><%flash.eachdo |role,message|%><%=render(partial:'shared/flash',locals:{role:role,message:message})%><%end%></div><%=yield%></body></html>

Turbo frames (instead of *.turbo_stream.erb)

When using Turbo Frames, while the controller response will be injected with the relevant turbo_stream tags, Turbo willonly render the matching Turbo Frame content, and ignore the turbo_streams. To get around this, you can utilize theturbo layoutturbo_rails/frame.html.erb and useturbo_stream_from to send the updates back to the browser. For example:

# app/views/layouts/turbo_rails/frame.html.erb<html><head><%=yield:head%></head><body><%flash.turbo.flashes.eachdo |role,message|%><%=Turbo::StreamsChannel.broadcast_prepend_tocurrent_user,:alerts,target:"alerts",partial:'shared/flash',locals:{role:,message:}%><%# customize this based on your project%><%end%><%=yield%></body></html>

Configuration

In an initializer (defaults are shown):

TurboFlash.configure do |config|  # make all flashes TurboFlash-flashes  # config.inherit_flashes = true     # clear the TurboFlash target if there are no flashes in a TurboStream response  # config.clear_target_unless_flashed = true     # the default TurboStream target element ID  # config.target = "flash"    # the default TurboStream action  # config.action = :update     # the default TurboStream partial  # config.partial = 'shared/flash'    # the default flash key variable name  # config.key = :role     # the default flash message variable name  # config.value = :message end

Gotchas

IfTurboFlash.configuration.inherit_flashes isfalse, and you want to copy over the regular flashes,you can invokeflash.turbo!(options = {}) to copy over the flashes that are currently stored in the session.

IfTurboFlash.configuration.clear_target_unless_flashed isfalse, and you would like to clear flashes in the TurboStreamresponse, you can invokeflash.turbo.clear_target! to clear the TurboStream target if there are no flashes.

If you want to clear all potential TurboFlashes, callflash.turbo.clear!

Contributing

There wasn't much thought put into this, but it works for me, so it might work for you!

License

The gem is available as open source under the terms of theMIT License.

About

Automagically include your flash messages in your Ruby on Rails Hotwire TurboStream responses.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp