ユーザーの回答が真か偽かをチェックする「checked」という数式があります (これは JavaScript である必要があります)。それが真の場合、レールでは、ユーザーと「状態」の属性を持つステップの間に新しい関係を作成する必要があります。作成したら、その状態を「保留中」から「完了」に変更する必要があります。ユーザーが次のボタンをクリックしたら、次のステップに進む必要があります。ルビーをJavaScriptに入れることができないと思うので、これを行う方法が非常に混乱しています。
question.html.erb
<script>
var checked = // this is a function that returns true or false based on whether user answer is correct - assume this works
if(checked)
{
//display correct-answer dialogue
$('#modal-simple-success').modal('show') <<< this works fine
//create a relationship between User and Step (UserStep Model)
<%= current_user.user_steps.build(step_id: @step.id)} %> << need help here considering cant have ruby in javascript
//after building the relationship between the two models, change state from pending to finished
<%= @user_steps.state.finish! %> <<need help here
//move to next step
$('#next-button').on('click', move to next step??? <<need help here
}
else
{
//display wrong-answer dialogue
$('#modal-simple-failure').modal('show')
}
</script>
これが私のステップのコントローラーです:
class StepsController < ApplicationController
def show
@course = Course.find(params[:course_id])
@level = Level.find(params[:level_id])
@step = Step.find(params[:id])
@user_step = current_user.user_steps.find(params[:id])
end
end
送信ボタンをこれから変更することを考えました:
<div class="checkAnswer" data-toggle="modal">
<%= link_to_function "check answer", 'execute();', class: "btn btn-success"%>
</div>
これに:
<div class="checkAnswer" data-toggle="modal">
<%= form_for(current_user.user_steps.build(step_id: @step.id), remote: true) do |f| %>
<div><%= f.hidden_field :step_id %></div>
<%= f.submit "check answer", class: "btn btn-success"%>
<% end %>
</div>
ただし、execute(); を開始する必要があります。機能し、状態を変更する方法がまだわかりません。
これについて正しい方法で助けていただければ幸いです。