Accidental Technologist

Musings about Entrepreneurship, Technology and Software Development

  • Home
  • About
  • Still River Software
  • Privacy Policy

Powered by Genesis

You are here: Home / Hotwire / How to Fix Rails Flash Rendering When Using Hotwire

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

Related

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

Care about your privacy? I do and use Fathom Analytics on this site.

Fathom Analytics

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

  • Everything I read these days mentions ChatGPT or AI as the answer to everything. So many red flags. I hate trends l… https://t.co/ITLxsfAvd9
    about 1 week ago
  • If anyone knows any projects looking for some dev help, mainly Rails, as a lead who can manage small teams…I’d love… https://t.co/u0ClZ8x5wB
    about 3 weeks ago
  • This is such the truth…Even Amazon can't make sense of serverless or microservices https://t.co/0zK9YVMdrj
    about 4 weeks ago
  • I’m not sure I’d subscribe to content on Twitter when I already follow someone. Maybe I’m missing something.
    about 1 month ago
  • This looks really cool and a great real world use of AI. https://t.co/DzX5BbgDdY
    about 1 month ago
  • RSS - Posts
  • RSS - Comments
Find me on Mastodon