Accidental Technologist

Musings about Entrepreneurship, Technology and Software Development

  • Home
  • About
  • Still River Software
  • Privacy Policy

Powered by Genesis

How to Fix Rails Flash Rendering When Using Hotwire

May 14, 2021 by admin Leave a Comment

Tweet

I added Hotwire to a Ruby on Rails application I’ve been working on and discovered some issues when rendering flash messages. Yesterday I wrote about problems related to CORS error using OmniAuth.  Today’s is not as exciting but still as annoying.

Problem

I was in the process of testing some validation changes I implemented and realized errors were not being displayed. I went through the typical debug scenarios and found that errors were being returned but just not displayed.

The code consists of just trying to create a user:

def create
  @user = User.new(user_params)
  if @user.save
    redirect_to root_path, notice: “User created successfully“
  else
    render :new
  end
end

When creating a user, the new form rendered but errors were not displayed.

Solution

The reason the messages were not being displayed is because of Turbo and Rails UJS conflicting. Solving the problem can be done in one of two ways.

1. It appears Turbo expects form submissions to redirect to a new location. When there is an error, we are staying on the same page. Adding status: :unprocessable_entity to the render fixes the problem.

def create
  @user = User.new(user_params)
  if @user.save
    redirect_to root_path, notice: “User created successfully“
  else
    render :new, status: :unprocessable_entity 
  end
end

2. A similar solution from yesterday’s post also works. Adding

data: { turbo: false }

The form declaration disables turbo and lets Rails UJS handle as desired.

I hope future versions of Turbo handle this better.

Share this:

  • LinkedIn
  • Twitter
  • Facebook
  • Email
  • More
  • Pinterest
  • Tumblr
  • Pocket
  • Reddit

Filed Under: Hotwire, Ruby on Rails Tagged With: Hotwire, Ruby on Rails, Turbo

Recent Posts

  • Social Media Times Are Changing
  • It has certainly been a long time…
  • How to Fix Rails Flash Rendering When Using Hotwire
  • Hotwire Fix for CORS Error when using Omniauth
  • Fix Installation of Ruby using rbenv on macOS Big Sur

Categories

Services I Love

HatchBox - Easy Rails Deploys Fathom Analytics
Follow @rbazinet

Rob Bazinet
@rbazinet

  • Great little utility https://t.co/6AgboJv6oI
    about 1 day ago
  • Funny watching the hate to show up in my time line for ChatGPT. People getting defensive about it.
    about 4 days ago
  • I just backed The SaaS Playbook, by Rob Walling on @Kickstarter https://t.co/1FpTWN9c9v
    about 4 days ago
  • This looks like an interesting project. I need to dig in a bit. https://t.co/gacEX9AmEn "Phlex — fast, object-orie… https://t.co/4AUL0Z0SBl
    about 5 days ago
  • I attempted to use ChatGPT today and asked a couple questions it couldn’t answer. I thought it was a magical black… https://t.co/uJGw0CgUAv
    about 5 days ago
  • RSS - Posts
  • RSS - Comments
Find me on Mastodon