[英]Rails - on click event - how to deal with race condition between 2 Ruby methods?
On my view I have a link_to
element "Start Quiz!" 在我看来,我有一个
link_to
元素“开始测验!” that calls a controller method (method A) that sets a variable "Phase" and redirects the user to a new static page. 调用控制器方法(方法A),该方法设置变量“阶段”并将用户重定向到新的静态页面。 In my CoffeeScript I have an event listener listening for clicks on this
link_to
element. 在我的CoffeeScript中,我有一个事件侦听器,侦听此
link_to
元素的单击。 The listener calls a different method (method B) on the backend that uses the "Phase" variable to do sth. 侦听器在后端使用“ Phase”变量执行其他操作(方法B)。
The problem: sometimes method B tries to use the "Phase" variable before it has been updated by the controller method A! 问题:有时方法B会在控制器方法A更新“ Phase”变量之前尝试使用它! Does anyone have a suggestion what the best way would be to ensure that the "Phase" variable was updated before using it for anything else?
是否有人建议最好的方法是确保将“ Phase”变量更新后再用于其他用途?
I'm a Rails beginner and would appreciate any pointers. 我是Rails的初学者,不胜感激。 :)
:)
EDIT: "Phase" is a variable I use to track the current question of my quiz. 编辑:“阶段”是一个变量,我用来跟踪我的测验的当前问题。 It's not dependent on any input, but it needs increment whenever a new page is rendered (which is triggered by the button click).
它不依赖于任何输入,但是无论何时呈现新页面(由按钮单击触发),它都需要增加。
EDIT2: I've now tried to solve this by introducing an instance variable flag "is_changed" to my controller and letting the method B sleep until the flag is set to true: EDIT2:我现在尝试通过向我的控制器引入实例变量标志“ is_changed”并让方法B睡眠直到标志设置为true来解决此问题:
#method B:
def send_answers
until controller.index_changed?
sleep(0.5)
end
...
do sth.
end
#method A (in controller):
def initialize
@index_changed = false
end
def index_changed?
@index_changed
end
def set_index_changed!
@index_changed = false
end
def switch_to_next_question
...
self.phase += 1
if self.save
Thread.new do
@index_changed = true
ActiveRecord::Base.connection.close
end
else
...
end
end
It's not working. 没用 Method A gets stuck sleeping.
方法A陷入睡眠状态。 How do I fix this?
我该如何解决? And is this even the right approach?
这甚至是正确的方法吗?
Is it a problem with the scope of my @index_changed variable? 我的@index_changed变量的范围是否有问题? I'm trying to create an instance variable that is accessible from multiple methods within the class, but individual to each instance of this class.
我正在尝试创建一个实例变量,该实例变量可从该类中的多个方法访问,但对于该类的每个实例都是单独的。
Ok, I solved it by finding a way to call the 2 methods sequentially. 好的,我通过找到一种顺序调用两种方法的方法解决了这一问题。 I ended up eliminating the on click listener completely and calling the ActionCable backend method directly , which was possible by turning it into a class method:
我最终完全消除了on click监听器,并直接调用ActionCable后端方法 ,可以通过将其转换为类方法来实现:
class QuizDataChannel < ApplicationCable::Channel
#method B:
def self.send_answers #self. makes it a class method
#...
#do something with phase
end
end
And in my controller: 在我的控制器中:
#method A:
def switch_to_next_question
#...
self.phase += 1
if self.save
QuizDataChannel.send_answers
else
#...
end
end
As you can see in the end I got rid of all of these: sleep
, new threads and the index_changed
flag. 如您所见,最后我摆脱了所有这些问题:
sleep
,新线程和index_changed
标志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.