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 theWarden::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 theDevise::Test::ControllerHelpers
module to inject therequest.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.
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
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.
Add
config.include Devise::Test::ControllerHelpers, type: :controller
in your rails_helper.rb file.
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)
...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With