dimanche 5 septembre 2021

Is it possible to access instance variables from controller to script?

I have this script inside my index.html.erb

<script>
document.getElementById('start-fight').addEventListener('click',function(){

 document.getElementById('player-one-button').disabled = true
  document.getElementById('player-two-button').disabled = true
 number = 5;
  randomNumber = Math.floor(Math.random()*10);

  if (randomNumber < 5 ){
  document.getElementById('amount-bet-for-player-one').innerHTML = "player one wins"
  @lasttransaction.result = "player-one"

  }
  else if(randomNumber > 5){
  document.getElementById('amount-bet-for-player-one').innerHTML = "player two wins"
  @lasttransaction.result = "player-one"
  }
  else{
  document.getElementById('amount-bet-for-player-one').innerHTML = "draw"
  @lasttransaction.result = "draw"
  }
  })     
  </script>

i have this in my index controller

   def index

@transactionhistories = Transactionhistory.all
@battles = @client.getPlayerByPowerstats
@battles2 = @client.getPlayerByPowerstats
@transactionhistory = current_user.transactionhistories.build
@lasttransaction = Transactionhistory.last
@user = current_user.id

end

Im trying to create an app where the user can place his bet to player one or player two . im trying to update the last transaction after i click the fight button . Is it possible to access the controller from script using vanilla javascript ?

Aucun commentaire:

Enregistrer un commentaire