2016-11-16 59 views
1

我試圖在ActionJob中通過ActionCable播放模板。ActionCable + Devise + Pundit + ApplicationController.render

ApplicationController.render(partial: "messages/message", locals: { message: message }, assigns: { current_user: user}).squish 

在大多數情況下,這工作得很好,但是我的一些模板在視圖中使用Punit進行授權。

<% if policy(message).show? %> 
    <%= message.body %> 
<% end %> 

這會在作業運行時產生錯誤。

ActionView::Template::Error: Devise could not find the `Warden::Proxy` instance on your request environment. 

快速谷歌搜索發現此問題:https://github.com/plataformatec/devise/issues/4271

中票和鏈接中提到,沒有ENV [「看守」]用,因爲沒有中間件已經執行添加它。

我該如何解決這個問題?

+0

您是否檢查過[this](http://www.thegreatcodeadventure.com/using-action-controller-renderers-in-rails-5-with-devise/)? –

+0

@ArunKumar是的,那是票中引用的鏈接之一。 –

回答

1

作爲解決辦法,這是我做了什麼:

class ActiveJobController < ActionController::Base 
end 

在我的部分,而不是使用policy幫手,我這樣做是

<% if Pundit::PolicyFinder.new(message).policy.new(current_user, message).show? %> 
    <%= message.body %> 
<% end %> 

,並從我的ActiveJob

ActiveJobController.render(partial: "messages/message", locals: { message: message, current_user: user }).squish 

這可以避免任何股票Devise和Pundit傭工參考電話env["warden"]。這不是理想的,但現在在請求和工作中都可以工作。

+0

你節省了我的一天。非常感謝。 – Chamnap