ruby-on-rails - 如何使用 Rails 5 中的 Application.renderer.render 和设计

标签 ruby-on-rails devise real-time render warden

我想实时制作我的应用程序

这是我的错误

ActionView::Template::Error (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. If you are seeing this on one of your tests, ensure that your tests are either executing the Rails middleware stack or that your tests are using the Devise::Test::ControllerHelpers module to inject the request.env['warden'] object for you.)
1: -if user_signed_in?
2: .ui.popup.computer{id:"post#{post.id}user#{post.user.id}", style:"padding:0px"}
3: .ui.card
4: .image



我不知道该怎么办

请帮帮我。

最佳答案

我想我为我的情况找到了解决方案。

定义 renderer_with_signed_in_user ApplicationController 中的类方法.

class ApplicationController < ActionController::Base
  ...
  def self.renderer_with_signed_in_user(user)
    ActionController::Renderer::RACK_KEY_TRANSLATION['warden'] ||= 'warden'
    proxy = Warden::Proxy.new({}, Warden::Manager.new({})).tap { |i|
      i.set_user(user, scope: :user, store: false, run_callbacks: false)
    }
    renderer.new('warden' => proxy)
  end
  ...
end

然后您可以从 Rails 应用程序的其他部分进行渲染,如下所示:
renderer = ApplicationController.renderer_with_signed_in_user(user)
renderer.render template: 'notifications/show', layout: false, locals: { foo: 'bar' }

感谢 Stefan Wienert 的 article

关于ruby-on-rails - 如何使用 Rails 5 中的 Application.renderer.render 和设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39128798/

相关文章:

ruby-on-rails - 在 Rails 4 中设置模型的默认显示字段

ruby-on-rails - gem 未加载 : NameError (uninitialized constant) | But gem works in local path

ruby-on-rails - 如何在不使用身份验证的情况下使用 rails 中的设计获取当前用户!在 Controller 上

ruby-on-rails - 从引擎安装时,Rails 应用程序找不到 Devise 函数

ruby-on-rails - 设计、Omniauth 和 'new with session'

multithreading - Hadoop 可以减少 SIFT 的运行时间吗?

mysql - Rails where 查询 has_and_belongs_to_many

ruby-on-rails - 我如何告诉 Rails 4 应用程序我的 SSL 证书在哪里?

Javascript实时访客信息

audio - 实时音频处理 - 延迟可行性检查