Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 尝试在Jquery选择器中使用Ruby,即在ERB文件中?_Ruby On Rails - Fatal编程技术网

Ruby on rails 尝试在Jquery选择器中使用Ruby,即在ERB文件中?

Ruby on rails 尝试在Jquery选择器中使用Ruby,即在ERB文件中?,ruby-on-rails,Ruby On Rails,在index.html.erb中,我有: <button type="submit" id="<%= Book.first.id %>">Activate</button> 激活 再往下看,我有: <script> $("#<%= Book.first.id %>").click(function(event){ // do something }); <script> $(“#”)单击(函数(事件

在index.html.erb中,我有:

<button type="submit" id="<%= Book.first.id %>">Activate</button>
激活
再往下看,我有:

<script>
$("#<%= Book.first.id %>").click(function(event){
     // do something
    });
<script>

$(“#”)单击(函数(事件){
//做点什么
});
但是,当我尝试呈现页面时,会出现以下错误:

undefined method `id' for nil:NilClass
$("#<%= Book.first.id %>").click(function(event){
nil:NilClass的未定义方法'id' $(“#”)单击(函数(事件){ 我得到这个错误是因为
还没有在页面上呈现吗?我对Jquery的了解表明,在我尝试单击它之前,我甚至不应该得到这个错误

我看了一下(自我回答)的答案:我认为我所做的是可能的


我缺少什么?

您可以使用类并获取属性id

<button type="submit" class="submit-btn" id="<%= Book.first.id %>"> Activate </button>

<script>
  $(".submit-btn").click(function(event){
     console.log($(this).attr('id'))
  });
<script>
激活
$(“.submit btn”)。单击(函数(事件){
console.log($(this.attr('id'))
});

您可以使用类并获取属性id

<button type="submit" class="submit-btn" id="<%= Book.first.id %>"> Activate </button>

<script>
  $(".submit-btn").click(function(event){
     console.log($(this).attr('id'))
  });
<script>
激活
$(“.submit btn”)。单击(函数(事件){
console.log($(this.attr('id'))
});

在这个问题上有很多问题

因为您从Rails得到了一个错误

您确定Book.first存在吗?您可以在控制台中进行测试

其他意见:

  • 激活前,此行中缺少结束标记


    在这个问题上有很多问题

    因为您从Rails得到了一个错误

    您确定Book.first存在吗?您可以在控制台中进行测试

    其他意见:

  • 激活前,此行中缺少结束标记


    老兄,我做错了。这个链接:正是我所需要的——而且是为像我这样的无知的人编写的,让他们理解。:)当你学习react rails gem时,如果你想要JS前端风格的组件和后端rails,一定要查看它。它写得很好,可以让你很快启动并运行react。伙计,我一直在做错事。这个链接:正是我所需要的——并且是为像我这样的新手编写的,以便于理解。:)当你学习时,一定要查看react rails gem,如果你想要JS前端风格的组件和后端rails。它写得很好,让你可以很快地在react上启动和运行。