2016-11-16 8 views
1

ActionCableでブロードキャストするために、ActionJobのテンプレートをレンダリングしようとしています。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. 

迅速なGoogle検索は、この問題を明らかに: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つでした。 –

答えて

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 

こうすることで、株式の募集人と賞与のヘルパー数はenv["warden"]です。これは理想的ではありませんが、要求とジョブでレンダリングされたときの両方で機能します。

+0

あなたは私の日を救った。どうもありがとう。 – Chamnap

関連する問題