Q:

Helper Devise: could not find the `Warden::Proxy` instance on request environment

I try to use Devise for my Rails app. I can sign up and login but when I go to my other page "build" I get the following error:

Devise::MissingWarden in Home#show 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.

Here is my controller:
class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception

  private
  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    build_path
  end
end

Here rea my two partial views:
<!-- views/devise/menu/_login_items.html.erb -->
<% if user_signed_in? %>
  <li>
  <%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
  </li>
<% else %>
  <li>
  <%= link_to('Login', new_user_session_path)  %>
  </li>
<% end %>

and
<!-- views/devise/menu/_registration_items.html.erb -->
<% if user_signed_in? %>
  <li>
  <%= link_to('Edit registration', edit_user_registration_path) %>
  </li>
<% else %>
  <li>
  <%= link_to('Register', new_user_registration_path)  %>
  </li>
<% end %>

After debugging, I figured out that the problem is coming from this line in my "show" controller: template = HomeController.render('layouts/_template')
Thanks for your help.

A:

Based on this SO answer you need to include the Devise::Test::ControllerHelpers module in your controller specs. Add the following to your rails_helper:
RSpec.configure do |config|
  config.include Devise::Test::ControllerHelpers, type: :controller
end

A:

Add
config.include Devise::Test::ControllerHelpers, type: :controller

in your rails_helper.rb file.

A:

It can be happening when you try to get Devise current_user in some part of code being managed by action cable. like a background job to render comment or something else.
you can resolve it by using something like following in your controller, since you cant access warden at a model or job level(according to my limited knowledge):
(I call a job right after creation of my comment in CommentsController create action, which calls a private method to render a comment partial containing edit and delete button, for which current_user was required)
 def create
@product = Product.find(comment_params[:product_id])
@comment = @product.comments.build(comment_params)
@comment.save!

gon.comment_id = @comment.id
gon.comment_user_id = @comment.user_id

ActionCable.server.broadcast "chat", comment: render_comment

render :create, layout: false
end

def render_comment
  CommentsController.renderer.instance_variable_set(:@env, {"HTTP_HOST"=>"localhost:3000", 
    "HTTPS"=>"off", 
    "REQUEST_METHOD"=>"GET", 
    "SCRIPT_NAME"=>"",   
    "warden" => warden})

  CommentsController.render(
    partial: 'comments/comment_detail',
    locals: {
      product: @product,
      comment: @comment
    }
  )
end

this will help you resolve warden issue, if you have used devise's current_user in that partial, it will give you the commentor user (as it should since that user initiated the rendering of partial). 
Now to solve this, if you have a front end framework you might need to fetch the current user from cookies in order to restrict some actions like edit/delete. but if you are working in pure rails the solution I came across is that you have to make a hidden field in the dom having current users id, and you will fetch that id for comparison in a script. you might need to access rails variables in javascript, for that you can use GON gem.
I know this answer might contain much more than asked but I've searched alot and no where I found a satisfactory solution to this problem, feel free to discuss.

