Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 682
nico_lrx Avatar asked Jan 28 '17 14:01

nico_lrx


5 Answers

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
like image 80
Obromios Avatar answered Oct 18 '22 23:10

Obromios


It can happen when you call Devise methods, like current_user, in code being managed by action cable. like a background job to render comment or something else.

You may resolve it by doing something like the following in your controller. It propagates warden to the the @env['warden'] variable so that Devise can use it:

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 the 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.

like image 33
Fazal Karim Avatar answered Oct 18 '22 21:10

Fazal Karim


Add

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

in your rails_helper.rb file.

like image 26
Neeraj Kumar Avatar answered Oct 18 '22 23:10

Neeraj Kumar


I had a similar problem when testing views where 'current_user' was present in some conditional.

I solved it with this:

let(:user) { FactoryGirl.create :customer }

before(:each) do
  allow(view).to receive(:current_user).and_return(user)
  ...
like image 8
Alfredo Roca Mas Avatar answered Oct 18 '22 22:10

Alfredo Roca Mas


Well, i have faced the same issue "devise could not find the warden :: proxy in actioncable" It can be resolved by passing the current user_id to your perform method in your job as you might be accessing the current user in your view which you rendering through broadcast and due to this issue was caused

like image 1
Asad Hameed Avatar answered Oct 18 '22 22:10

Asad Hameed