Rails : 在 ruby 内获取 JS 变量 inside inline js

Rails : get JS variable inside ruby inside inline js

我需要根据用户单击的 link 更改一些 div 内容。 这是我的代码

:javascript
  $('.some_clickable_item').on('click', function(e) {
    clicked_element_id=$(this).data("element_id")
    $('#my_destination_div').html("#{ render partial: "dashboard/some_partial_name", locals: {item_id: clicked_element_id} }");
  })

显然,这是行不通的,因为 clicked_element_id 是一个 js 变量,但我需要将它传递给我的部分变量。

有什么简单的想法可以让它发挥作用吗? 谢谢!!

部分渲染发生在 click 事件之前。事实上,在浏览器收到响应之前,部分已在服务器上呈现。

您必须使用 XHR 来替换 #my_destination_div 的内容。参见 https://api.jquery.com/load/

.load( url [, data ] [, complete ] )

Description: Load data from the server and place the returned HTML into the matched element.

This method is the simplest way to fetch data from the server.

https://api.jquery.com/load/