dimanche 4 novembre 2018

how mix a var in js to a code of ruby on rails in a view

i want something like this:

enter image description here

enter image description here

polo and rere are country

all i need is get the information of what country the user want to be moderate, but i can not take the information because i can t mix code on ruby on rails and code html, js so i really don t know what can i do?

i search for a lo of foros but i can t find a code that let me mix both kind of code to do somthing like this

<%=Country.find(document.getElementById("demo").innerHTML)%>

here are my html code:

<label for="petition">do your petition to be moderator of some country:</label>

<div class="select you country moderate">
<button id="btn1" onclick="myFunction()" class="dropbtn">select country</button>
  <div id="myDropdown" class="dropdown-content">
    <% Country.all.each do |country| %>

        <button onclick=myFunction2("<%= country.name %>","<%= country.id %>") ><%= country.name %></button>

    <%end%>
  </div>
</div>


<form action="/" >

    <button name="petition" value="<%=Country.find(document.getElementById("demo").innerHTML)%>" type="submit">ask for be moderator</button>

</form>

and here js code:

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {

    document.getElementById("myDropdown").classList.toggle("show");

}
function myFunction2(country_choose,id_country) {
    document.getElementById("btn1").innerHTML=country_choose
        var x = (id_country);
    document.getElementById("demo").innerHTML = x; 
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>

and here i put the controller:

class WelcomeController < ApplicationController
  before_action :set_welcome, only: [:show]
  protect_from_forgery :except => :receive_guest
  helper_method :current_or_guest_user

  # GET /users
  # GET /users.json
  def index
    @countries=Country.all




    puts params[:petition]
end

error of the view:

undefined local variable or method `document' for #<#<Class:0x00005616768bb6f8>:0x00007f12e8e48d90>

Extracted source (around line #67):


<form action="/" >

    <button name="petition" value="<%=Country.find(document.getElementById("demo").innerHTML)%>" type="submit">ask for be moderator</button>

</form>




Aucun commentaire:

Enregistrer un commentaire