How to use Aplication.renderer.render from rails 5 with the application

I want to make my application in real time

That's my fault

ActionView :: Template :: Error (Devise could not find an instance of Warden::Proxy in the query environment. Make sure your application loads Devise and Warden as expected, and that the Warden::Manager middleware is present in your middleware stack If you see this in one of your tests, make sure your tests either run the Rails middleware stack, or your tests use the Devise::Test::ControllerHelpers module to enter 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

I do not know what to do

Help me please.

+7
ruby-on-rails real-time devise render warden
source share
2 answers

I think I found a solution for my business.

Define the class method renderer_with_signed_in_user in the 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) } renderer.new('warden' => proxy) end ... end 

And then you can display from other parts of the Rails application, for example:

 renderer = ApplicationController.renderer_with_signed_in_user(user) renderer.render template: 'notifications/show', layout: false, locals: { foo: 'bar' } 

Credit to Stefan Wienert for his article

0
source share

When you use the new Renderer Rails 5, middleware fails. Devise uses Warden and sets it as an env ['warden'] environment variable, and thus it is not present when the renderer is called. This is the reason you get this error.

To make it work, on your controller, simply use before_action for the controller action #, which will be displayed to set and pass the instance variable needed for the view.

If you need to check if the user has been logged in or use current_user in the rendered view:

 class ExamplesController < ApplicationController before_action :user_logged_in?, only: :show before_action :set_user, only: :show def show # whatever the action does end private def user_logged_in? @user_logged_in = user_signed_in? end def set_user @user = current_user end end 

Then in the View ExampleController # show:

 # views/examples/show.html.erb <%= "Online" if @user_logged_in %> <%= @user.full_name %> 

Hope that helps

-one
source share

All Articles