I am currently developing a new project where the backend involves Users who have multiple "Courses" each containing multiple "Steps".
My goal is to create a JavaScript function that validates the user's answer. For example, if the user inputs "6", which is the correct answer, I envision the matching function to look something like this:
function match_answer(course, answer){
}
If the JavaScript event confirms the correct answer, I would like it to update the state_machine on the backend. Here is an overview of the 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 behind wanting to use a JavaScript event to change the state of the state machine is to ensure that when the user logs back in, they are taken to the appropriate place in the course.
1) How can I modify the state of the state_machine with a JavaScript event? Specifically, if the match_answer function returns true, how can I switch the state from :pending to :finished?
2) Assuming the correct answer is stored in the database and will be compared against the user's input, how can I retrieve the correct answer from the database to incorporate it into the match_answer JavaScript function for validation?
Any assistance or guidance you can provide on this matter would be greatly appreciated.