如何定义实例模型在 Rails 6 中的呈现方式 [英] How to define the way instance model should be rendered in Rails 6
问题描述
我正在使用 Hotwire 并在 Rails 6.0.3.6 应用程序上设计用户.
I'm using Hotwire and devise User on a Rails 6.0.3.6 app.
我为用户添加此广播
class User
after_create_commit { broadcast_append_to 'users' }
end
然后当一个用户被创建时,它会通过他的部分(users/user),在那里我试图使用 current_user
设计助手.
Then when a User is created it goes through his partial (users/user) where I am trying to use current_user
Devise helper.
但是 Rails 抛出这个错误:
But Rails throw this error:
Devise::MissingWarden in Players::Registrations#update
Devise could not find the `Warden::Proxy` instance on your request environment.
Make sure that your application is loading Devise and Warden as expected and that the `Warden::Manager` middleware is present in your middleware stack.
也许解决方案是能够告诉 Rails 以下内容:
Maybe the solution would be to be able to tell Rails the following:
如果从模型中,您试图渲染他的默认部分 (.to_partial_path),您需要向它传递一些局部变量,例如 user: self
.
If from a model, you are trying to render his default partial (.to_partial_path) you need to pass it some locals variables,like user: self
.
我怎么能这样对 Rails 说?
How could I say that to Rails ?
谢谢你的帮助
推荐答案
DHH 说:
用于 Turbo 流的部分必须是全局的引用,因为它们是由 ApplicationRenderer 呈现的,而不是在特定请求的上下文.
Partials used for turbo streaming have to be free of global references, as they’re rendered by the ApplicationRenderer, not within the context of a specific request.
https://discuss.hotwire.dev/t/authentication-and-devise-with-broadcasts/1752/4
这篇关于如何定义实例模型在 Rails 6 中的呈现方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!