简体   繁体   English

如何在 javascript 中使用变量 escape_javascript rails

[英]How to use variable in javascript escape_javascript rails

I found two other posts was the same subject in stackoverflow: How to use variable in javascript escape_javascript rails?我在stackoverflow中发现另外两个帖子是同一主题: How to use variable in javascript escape_javascript rails? and How to use variable in javascript escape_javascript rails?以及如何在 javascript escape_javascript rails 中使用变量?

I applied the solutions exposed, and not only did not work, but something weird happened.我应用了暴露的解决方案,不仅没有工作,而且发生了一些奇怪的事情。 Well, at least I think it is weird that's a Ruby variable outputs one result when inspected and a totally different result when printed.好吧,至少我认为 Ruby 变量在检查时输出一个结果而在打印时输出完全不同的结果是很奇怪的。

I am developing a rails 6 application where The contents of the Files are as follows:我正在开发一个 rails 6 应用程序,其中文件的内容如下:

something.html.erb某事.html.erb

<%= form_with(url: "/graficos/partido", method: "get") do |f| %>
    <%= f.button "ClickMe",
           :class => "btn btn-secondary text-white mr-1 active",
           :value => "Clicked",
           :data  => {
                 disable_with: '<i class="fa fa-spinner fa-spin"></i>'.html_safe
            }
          %>
  <% end %>

  <article class="col-9">
    <div id="resultquery">
    </div>
  </article>

something.js.erb东西.js.erb

var x=[1,2,3]
x=JSON.stringify(x)
var yt = 99

  $("#resultquery").html("<%= j (render :partial => ‘something’,
                                        :locals => {:xt => '_xt_',
                                                    :yt => " + yt + "
                                                    }
                                ).html_safe
                          %>".replace("_xt_", x ));

_something.html.erb _something.html.erb

<p><%= xt.inspect    %></p>
<p><%= xt.class    %></p>
<p><%= xt   %></p>
<p><%= yt   %></p>

And now comes the weird part.现在是奇怪的部分。 The output on the screen shows the following屏幕上的output显示如下

"[1,2,3]"
String
_xt_
+ yt +

================================================================== ==================================================== =================

In this example, the approach " …:yt => " + yt + ", … “ Generates the result "+ yt +" (the string itself instead of the value of yt). Jorawar Singh suggested “=+ yt +-”, Even though I don't understand what this means, it didn't worked to.在这个例子中,方法 "…:yt => " + yt + ", … " 生成结果 "+ yt +"(字符串本身而不是 yt 的值)。Jorawar Singh 建议“=+ yt +-” , 即使我不明白这是什么意思,但它没有起作用。

The placeholder approach (.replace(" xt ", x )) generates the weird result:占位符方法 (.replace(" xt ", x )) 会产生奇怪的结果:

<p><%= xt.inspect    %></p> gives "[1,2,3]", the value I expected to get
<p><%= xt   %></p>          gives _xt_ (xt[0] => “_”, xt[1] => “x”, ...), the string to be replaced in the  something.js.erb file.

My questions are (q1) How can I pass a parameter since the other approaches didn't work.我的问题是(q1)我如何传递参数,因为其他方法不起作用。 I did something wrong?我做错事情了? (q2) What happened in the placeholder approach? (q2) 占位符方法发生了什么? I tried it without stringify and the first result was "1,2,3", and the weird second output was the same.我在没有stringify的情况下尝试了它,第一个结果是“1,2,3”,奇怪的第二个output是一样的。

After lots of research, I was able to find out why the presented approaches do not work.经过大量研究,我能够找出为什么所提出的方法不起作用。 The problem is a misconception about the client-server model.问题是对客户端-服务器 model 的误解。

When the server finds escape to Ruby (<%=... %>), it replaces the contents with something.当服务器发现逃逸到 Ruby (<%=... %>) 时,它会用一些东西替换内容。 In other words: the client do not receive whatever is inside the Escape to Ruby.换句话说:客户端不会收到 Escape to Ruby 中的任何内容。 With this in mind it's easy to see that the first approach ( <%= …:yt => " + yt + " … %>) is doomed: the contents inside the Escape to Ruby will be replaced by the server and so will never arrive the client in this format.考虑到这一点,很容易看出第一种方法( <%= ...:yt => " + yt + " ... %>)注定要失败:Escape to Ruby 中的内容将被服务器替换,因此永远不会以这种格式到达客户。 In this particular case the server will concatenate all the strings generating something like “... :yt => + yt + …” that will be sent to the client.在这种特殊情况下,服务器将连接所有生成类似“... :yt => + yt + ...”的字符串,这些字符串将被发送到客户端。

The second approach is feasible since the server generates and sends to the client a string containing something like “... :xt => ' xt ' … ”.第二种方法是可行的,因为服务器生成并向客户端发送一个包含类似“... :xt => ' xt ' ... ”的字符串。 The client into in it's turn to work replaces the xt by whatever it wants to be the value of the parameter.轮到它工作的客户端将xt替换为它想要的参数值。 The problem is that it behaves like Schrodinger's cat: It's value becaames both ' xt ' and "[1,2,3]".问题是它的行为就像薛定谔的猫:它的值是' xt '和“[1,2,3]”。 This must be some Ruby internals, and I have no clue why happens.这一定是一些 Ruby 内部,我不知道为什么会发生。

Anyway, since both methods do not work I engineered a solution by using actions in a controller that was created just to keep the values of the parameters.无论如何,由于这两种方法都不起作用,我通过使用 controller 中的操作设计了一个解决方案,该操作是为了保留参数值而创建的。 In the JavaScript portion of the code, I send an Ajax request to set the value of the parameters in a controller#action class variables.在代码的 JavaScript 部分中,我发送了一个 Ajax 请求来设置控制器#action class 变量中的参数值。 Inside The View I get the controller#action class variable values with a Json request.在视图中,我通过 Json 请求获得了 controller#action class 变量值。

Ugly(?), but works.丑陋(?),但有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM