简体   繁体   English

如何将 json 数据从 ruby controller 传递到 javascript(导轨)?

[英]How to pass json data from ruby controller to javascript (Rails 6.1)?

I'm trying to pass some json data from my database to front-end so that I can manipulate those with javascript!我正在尝试将一些 json 数据从我的数据库传递到前端,以便我可以使用 javascript 操作这些数据! I started by creating some temporary json data in my home.html.erb as follows:我首先在我的home.html.erb中创建了一些临时的 json 数据,如下所示:

<%= javascript_tag do %>
    window.testJSON_data = 
    [
        {
            "name" : "Bill Gates",
            "telephone" : 123456789
        },
        {
            "name" : "Elon Musk",
            "telephone" : 987654321
        }
    ];
    console.log(testJSON_data); /* Works as expected - will print JSON data in console! */
<% end %>

The code above will print the correct data and JSON format on the console log.上面的代码将在控制台日志上打印正确的数据和 JSON 格式。 But I find some issues while I try to use my database data with the same approach.但是当我尝试以相同的方法使用我的数据库数据时,我发现了一些问题。 I started by passing the correct data to my home controller.我首先将正确的数据传递给我的家庭控制器。 The ruby code in the controller below will print all data on the browser page using the render function, but obviously that's not what I want to do.下面 controller 中的 ruby 代码将使用render function 在浏览器页面上打印所有数据,但显然这不是我想做的。 So I commented the render command and tried to capture these data with my home.html.erb于是我注释了render命令,并试图用我的home.html.erb捕获这些数据

class HomeController < ApplicationController
  def index
    @data = EmergencyFriend.all
    @jsonData = JSON.pretty_generate(@data.as_json)

    #render json: @jsonData
  end
end

I have tried this code which almost prints the data as I want but not as an object.我已经尝试过这段代码,它几乎可以按照我的意愿打印数据,但不是 object。 I have also tried these, but none will print data as object:我也尝试过这些,但没有一个会将数据打印为 object:

  • console.log(JSON.stringify('<%= j @jsonData.as_json.html_safe %>'));
  • console.log(JSON.stringify('<%= j @jsonData.as_json %>'));
  • console.log(JSON.stringify('<%= j @jsonData %>'));
<%= javascript_tag do %>
    console.log('<%= j @jsonData %>');
<% end %>

WILL RETURN:

[
  {
    &quot;id&quot;: 1,
    &quot;first_name&quot;: &quot;Emergency Contact&quot;,
    &quot;last_name&quot;: &quot;01&quot;,
    &quot;phone_number&quot;: 150431,
    &quot;email&quot;: &quot;user_01@email.com&quot;,
    &quot;twitter&quot;: &quot;@user01&quot;,
    &quot;created_at&quot;: &quot;2021-06-08T12:20:46.298Z&quot;,
    &quot;updated_at&quot;: &quot;2021-06-08T13:04:28.480Z&quot;,
    &quot;user_id&quot;: 1,
    &quot;country_code&quot;: &quot;CY&quot;,
    &quot;dial_code&quot;: 357
  },
  {
    ...
  },
  {
    ...
  }
]

I know is something stupid but I have been trying for an hour to find a solution and I cannot, It's either passing the data wrongly or printing the data wrongly.我知道这很愚蠢,但我已经尝试了一个小时来寻找解决方案,但我做不到,要么错误地传递数据,要么错误地打印数据。 but I do not know which is the case here.但我不知道这里是哪种情况。

What should I do to store JSON data from ruby controller to JavaScript?我应该怎么做才能将 JSON 数据从 ruby controller 存储到 Z686155AF75A60ED3F6E9D80CF1Z?

Use html_safe or raw method on that string.在该字符串上使用html_saferaw方法。

<%= @jsonData.to_s.html_safe %>

Consider also fetching it by ajax request.还可以考虑通过 ajax 请求来获取它。

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

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