如何将js中的var与视图中的rails上的ruby代码混合

时间:2018-11-04 20:09:47

标签: javascript html ruby-on-rails web model-view-controller

我想要这样的东西:

enter image description here

enter image description here

polo和rere是国家/地区

我所需要的只是获取用户希望成为哪个国家/地区的信息,但是我无法获取该信息,因为我无法在Rails上混合Ruby代码和html,js代码,所以我真的不知道该怎么做我会吗?

我搜索了很多问题,但是我找不到一个可以让我混合两种代码来进行类似操作的代码

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

这是我的html代码:

<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>

这里是js代码:

<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>

在这里我放控制器:

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

视图错误:

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>

0 个答案:

没有答案