Helper Devise: could not find the `Warden::Proxy`

2019-06-15 07:38发布

问题:

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.

回答1:

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


回答2:

Add

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

in your rails_helper.rb file.



回答3:

I had this error and could not find answer anywhere else. It was after dropping and creating the db. But I didn't load the schema.

This fixed it:

rake db:drop db:create db:schema:load


回答4:

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



回答5:

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


回答6:

I came across this error when the database was not migrated. The below command did the trick.

./bin/bundle exec rake db:migrate