[英]Resque job undefined method 'current_user'
It's my first attempt at Resque and doing background jobs, and I'm kind of stuck. 这是我第一次尝试Resque并做后台工作,但我有点受阻。
I have two issues at hand. 我手头有两个问题。
It gives me the error 它给我错误
undefined local variable or method `current_user'
Here is my code: 这是我的代码:
schedules_controller.rb schedules_controller.rb
def trial
@schedule = current_user.schedules.new
if @schedule.save
user = User.find(params[:id])
@client = Buffer::Client.new(user.token)
Resque.enqueue(ScheduleTweets, @client)
@schedule.update_attribute(:trial, true)
flash[:notice] = "success"
redirect_to :back
else
flash[:alert] = "Try again."
redirect_to :back
end
end 结束
and the worker: 和工人:
apps/workers/schedule_tweets.rb apps / workers / schedule_tweets.rb
class ScheduleTweets
@queue = :schedules_queue
def self.perform(client)
user = User.find(params[:id])
client = Buffer::Client.new(user.token)
@list = List.first(6)
@profiles = client.profiles
@profile_ids = profiles.map(&:id)
@list.each do |list|
client.create_update(body: {text: "#{list.text}", profile_ids: @profile_ids })
end
end
end
end
my thought process is that the client is the core of the entire process and should thus be the one. 我的思维过程是,客户是整个过程的核心,因此应该成为客户的整个过程。 However @profile_ids also contains anywhere from 1-5 values. 但是,@profile_ids也包含1-5个值。
When I run the task I get the undefined local variable or method 'current_user'. 当我运行任务时,会得到未定义的局部变量或方法“ current_user”。 How do I fix that, also am I doing it right by choosing the @client as the thing to add? 我该如何解决这个问题,也可以通过选择@client作为添加对象来做到这一点吗?
Try this schedules_controller.rb 试试这个schedules_controller.rb
def trial
@schedule = current_user.schedules.new
if @schedule.save
user = User.find(params[:id])
@client = Buffer::Client.new(user.token)
Resque.enqueue(ScheduleTweets, user.id)
@schedule.update_attribute(:trial, true)
flash[:notice] = "success"
redirect_to :back
else
flash[:alert] = "Try again."
redirect_to :back
end
end
and the worker: 和工人:
apps/workers/schedule_tweets.rb apps / workers / schedule_tweets.rb
class ScheduleTweets
@queue = :schedules_queue
def self.perform(user_id)
user = User.find(user_id)
client = Buffer::Client.new(user.token)
@list = List.first(6)
@profiles = client.profiles
@profile_ids = @profiles.map(&:id)
@list.each do |list|
client.create_update(body: {text: "#{list.text}", profile_ids: @profile_ids })
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.