3

我有一个新的 rails 7 应用程序 [rails new devisetest],一个带有静态页面的简单控制器,并添加了设计 [gem 'devise' + rails g devise user]。所有默认值。根据设计说明将 Flash 消息添加到 application.html.erb

没有显示设计错误消息,但我看到它们是在控制台中生成的(或者至少,我看到呈现的消息但它们没有显示,并且我看到为 _links 部分呈现并且确实显示)在另一个测试中使用快速脚手架(并且已将设计视图复制到应用程序但未修改它们),显示脚手架的闪存消息但仍不显示设计。

如果我访问 http://localhost:3000/users/sign_up 并创建一个密码太短的用户,我会在日志中看到这一点

Started POST "/users" for ::1 at 2021-12-24 08:02:10 +0000
Processing by Devise::RegistrationsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"asdf@asdf.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (0.1ms)  begin transaction
  User Exists? (2.7ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ?  [["email", "asdf@asdf.com"], ["LIMIT", 1]]
  TRANSACTION (0.2ms)  rollback transaction
  Rendering layout layouts/application.html.erb
  Rendering /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/registrations/new.html.erb within layouts/application
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/shared/_error_messages.html.erb (Duration: 0.7ms | Allocations: 582)
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 92)
  Rendered /home/sroot/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/devise-4.8.1/app/views/devise/registrations/new.html.erb within layouts/application (Duration: 3.9ms | Allocations: 2199)
  Rendered layout layouts/application.html.erb (Duration: 77.5ms | Allocations: 4429)
Completed 200 OK in 473ms (Views: 78.7ms | ActiveRecord: 3.1ms | Allocations: 15288)

应显示错误消息的视图。 显示设计链接,但不显示消息

谁能指出我的错误?或者建议我可以遵循的其他步骤来更接近我的错误?

谢谢

4

2 回答 2

2

我有同样的问题。它在某种程度上与 Rails 7.0 和 Devise 4.8.1 的兼容性有关。我无法注册用户。sqlite3 数据库也仍然是空的,所以我不认为它只是闪存消息的问题并且没有输入用户。有些人通过添加以下内容来解决 Rails 7 / Devise 的各种问题config/initializers/devise.rb

config.navigational_formats = ['*/*', :html, :turbo_stream]
于 2022-01-04T10:36:35.210 回答
0

我正在关注教程(视频:https ://gorails.com/episodes/devise-hotwire-turbo?autoplay=1 )并在此处使用本教程进行了尝试:https ://medium.com/@nejdetkadir/how -to-use-devise-gem-with-ruby-on-rails-7-33b89f9a9c13

但是flash消息还是没有显示出来。

我现在添加到我的 devise.rb 中:

    # frozen_string_literal: true

    class TurboFailureApp < Devise::FailureApp
      def respond
        if request_format == :turbo_stream
          redirect
        else
          super
        end
      end

      def skip_format?
        %w(html turbo_stream */*).include? request_format.to_s
      end
    end


config.navigational_formats = ['*/*', :html, :turbo_stream]

  config.warden do |manager|
    manager.failure_app = TurboFailureApp
  #   manager.intercept_401 = false
  #   manager.default_strategies(scope: :user).unshift :some_external_strategy
  end

以及创建了一个 devise_controller.rb:

class Users::DeviseController < ApplicationController
    class Responder < ActionController::Responder
      def to_turbo_stream
        controller.render(options.merge(formats: :html))
      rescue ActionView::MissingTemplate => error
        if get?
          raise error
        elsif has_errors? && default_action
          render rendering_options.merge(formats: :html, status: :unprocessable_entity)
        else
          redirect_to navigation_location
        end
      end
    end
  
    self.responder = Responder
    respond_to :html, :turbo_stream
  end

于 2022-01-22T11:38:33.927 回答