Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display Devise login form errors when using Hotwire Rails

I've followed this GoRails video to get Devise working with hotwire-rails. I'm at a loss as to why my login error messages do not work the same way as in the video. The error messages work great on the registration form but on the login form I get this error:

Processing by Devise::SessionsController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
Completed 401 Unauthorized in 2ms (ActiveRecord: 0.0ms | Allocations: 406)

As Chris suggests in the video I've edited my Devise initializer to look like this.

My package.json has @hotwired/turbo-rails": "^7.0.0-beta.5, but in Chris's source code for the episode it's beta.3 so that does not seem to be the problem.

What am I missing here?

like image 553
Lee McAlilly Avatar asked Mar 07 '21 19:03

Lee McAlilly


Video Answer


2 Answers

I was having the same problem and it turns out there is no trigger to navigate. Appending :turbo_stream to this line in initializers/devise.rb solved it for me:

Devise.setup do |config|
  [...]
  config.navigational_formats = ['*/*', :html, :turbo_stream]
  [...]
end
like image 174
Loran Avatar answered Sep 26 '22 10:09

Loran


I got this working by unifying all of my flash errors / notices into one partial rather than having a flash notice partial and a separate block for form errors:

I put this in my layouts/application.rb:

<%= render "shared/notices" %>

That partial looks like this:

<% flash.each do |msg_type, message| %>
  <div class="alert">
    <div class="container">
      <button class="close" data-dismiss="alert"><span>×</span></button>
      <%= message %>
    </div>
  </div>
<% end %>

Then, my form_for in views/devise/sessions/new.html.erb looks like this: <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>

like image 36
Lee McAlilly Avatar answered Sep 22 '22 10:09

Lee McAlilly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!