简体   繁体   English

使用 Hotwire Rails 时如何显示 Devise 登录表单错误

[英]How to display Devise login form errors when using Hotwire Rails

I've followed this GoRails video to get Devise working with hotwire-rails .我已经按照这个 GoRails 视频让 Devise 与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"=>"elvis@example.com", "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 .正如 Chris 在视频中建议的那样,我将 Devise 初始化程序编辑为如下所示

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.我的 package.json 有@hotwired/turbo-rails": "^7.0.0-beta.5 ,但在 Chris 的源代码中它是beta.3 ,所以这似乎不是问题。

What am I missing here?我在这里想念什么?

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:将:turbo_stream 添加到 initializers/devise.rb 中的这一行为我解决了这个问题:

Devise.setup do |config|
  [...]
  config.navigational_formats = ['*/*', :html, :turbo_stream]
  [...]
end

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:我通过将我所有的 flash 错误/通知统一到一个部分而不是让 flash 通知部分和一个单独的表格错误块来完成这项工作:

I put this in my layouts/application.rb :我把它放在我的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|然后,我在views/devise/sessions/new.html.erb中的form_for看起来像这样: <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> %>

My solution is that you don't use app/javascript/packs/your_application.js in the layout of sign_in page.我的解决方案是您不要在登录页面的布局中使用app/javascript/packs/your_application.js

Instead, you can write a new js like app/javascript/packs/blank.js , in this blank.js , don't add import "@hotwired/turbo-rails" .相反,您可以编写一个新的 js,例如app/javascript/packs/blank.js ,在这个blank.js中,不要添加import "@hotwired/turbo-rails"

And use the blank.js in the new layout file app/views/layouts/blank.html.erb for the Devise sign_in page, like these:并将新布局文件app/views/layouts/blank.html.erb blank.js的 blank.js 用于 Devise sign_in页面,如下所示:

<head>
    <%= javascript_pack_tag 'blank' %>
    <%= stylesheet_pack_tag 'blank' %>
</head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM