简体   繁体   English

JS事件触发状态机中的后端状态更改(导轨)?

[英]JS event triggers a backend state change in a state machine (rails)?

I am working on a new project: the backend has Users which has_many "Courses" which also has_many "Steps". 我正在开发一个新项目:后端的用户具有has_many“ Courses”,也has_many“ Steps”。

I want javascript to validate the user's answer - ie let's say the user enters "6", which is the right answer. 我希望javascript验证用户的答案-也就是说,假设用户输入“ 6”,这是正确的答案。 I would imagine the function to check would look something like this. 我可以想象要检查的功能看起来像这样。

function match_answer(course, answer){
}

If the JS event proves true I would like it to change the state_machine on the backend. 如果JS事件证明是正确的,我希望它更改后端的state_machine。 The state_machine is as follows: state_machine如下:

class Step < ActiveRecord::Base
...

  state_machine initial: :pending do
    state :pending, value: 0
    state :finished, value: 1

    event :finish do
      transition :pending => :finished
    end

    event :restart do
      transition :finished => :pending
    end
  end
end

The reason why I want a javascript event to change the state of a state machine is because when the user logs back on I would like them to go the appropriate place in the course. 我希望javascript事件更改状态机状态的原因是,当用户重新登录时,我希望他们在课程中走到适当的位置。

1) How do I change the state of the state_machine with a javascript event? 1)如何通过javascript事件更改state_machine的状态? More specifically, if match_answer function proves true, how do I change the state from :pending to :finished. 更具体地说,如果match_answer函数被证明是正确的,如何将状态从:pending更改为:finished。

2) Assuming the correct answer(which will check against the user's answer) is stored in the database, how do I go about getting the correct answer from the database in order to implement it in the match_answer javascript function to check against the user's answer? 2)假设正确的答案(将检查用户的答案)存储在数据库中,我该如何从数据库中获取正确的答案,以便在match_answer javascript函数中实现该功能以检查用户的答案?

Any help or resources you can point me to would be very much appreciated. 您可以向我提供的任何帮助或资源将不胜感激。

1) In your match_answer javascript function, use an AJAX post to your server application to update what step they're on. 1)在您的match_answer javascript函数中,使用AJAX发布到您的服务器应用程序以更新它们所处的步骤。

2) In your javascript, you'll need to know the right answer, or possibly a list of questions and correct answers. 2)在您的JavaScript中,您需要知道正确的答案,或者可能是问题列表和正确答案。 One way to get this information would be to use the gon gem, which basically lets you set data in your controller just like an @instance variable, only it's made available in your javascript. 获取此信息的一种方法是使用gon gem,它基本上使您可以像@instance变量一样在控制器中设置数据,只有javascript中才可用。 A drawback would be that the answers would be visible if a user ever did a View Source on the page and looked hard enough. 缺点是,如果用户曾经在页面上执行过“查看源代码”并且看上去足够努力,则答案将是可见的。 Another way would be to fetch them separately from the server via an AJAX get request, but again the user might be able to snoop the traffic and find them, if that seems at all likely. 另一种方法是通过AJAX get请求从服务器中分别获取它们,但是如果看起来很可能的话,用户仍然可以监听流量并找到它们。

Maybe a better approach, since you're going to hit the server anyway for each answer, would be to use AJAX to post the user's answer to the server. 也许是一种更好的方法,因为无论如何您要为每个答案命中服务器,所以将使用AJAX将用户的答案发布到服务器。 If it's right, the server can update the step the user is on and return a 200 status code. 如果正确,则服务器可以更新用户所在的步骤并返回200状态代码。 If it's a wrong answer, the server can return a 422 status code. 如果回答错误,则服务器可以返回422状态代码。 Your ajax can use the success callback or the error callback from the ajax request to respond appropriately. 您的ajax可以使用ajax请求中的成功回调或错误回调来适当地响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM