簡體   English   中英

如何在Javascript中訪問Rails變量?

[英]How can I access Rails variable in Javascript?

我有一個Rails變量,我需要提取所有日期和時間(send_time)並將其放入JS數組。 我正在嘗試使用send_times的JS數組來阻止用戶將來使用這些日期/時間。 考慮安排一小時的會議室。 兩個會議不能同時進行。

我已經檢查了我的JS中的變量。

var taken = <%= @taken.inspect.html_safe %>;

這是檢查的變量

    var taken = [

    #<ScheduledRequest _id: 5b7f6b2842195a000400000f, send_time: 2018-08-25 18:00:00 UTC, sent_time: nil, warning_time: 2018-08-25 16:30:00 UTC, actual_send_time: 2018-08-25 16:45:00 UTC, processing: nil, retries: 10, pickup_address: "2209 West Anderson Lane, Boston, MA, USA", dropoff_address: "2209 West Anderson Lane, Boston, MA, USA", round_trip: false, token: nil, pickup_location: [-37.729439, 40.355348], dropoff_location: [-47.729439, 30.355348], notes: "Scheduled: Simple assembly and take away trash.", ip: "XHgw1nn3xIHzfs5t2xzZ/ez0Tj5gg=", notify_email: "", notify_phone: "", user_id: "53f4b008", thingy_type: "Blue Row", thingy_request_id: nil, error: nil, two_hour_warning_sent: nil, identifier: nil, estimated_time: "", thingy_request_log: nil, for_customer_api: false, removed: false, edit_date: "2018-08-25", edit_hours: "01", edit_minutes: "00", edit_ampm: "PM">, 

    #<ScheduledRequest _id: 5b7f92964029e90004000013, send_time: 2018-08-28 01:00:00 UTC, sent_time: nil, warning_time: 2018-08-27 23:30:00 UTC, actual_send_time: 2018-08-27 23:45:00 UTC, processing: nil, retries: 10, pickup_address: "5501 Spicewood Springs Road, Boston, MA, USA", dropoff_address: "2345 West Anderson Lane, Boston, MA, USA", round_trip: false, token: nil, pickup_location: [-77.771753, 20.384814], dropoff_location: [-57.7322452, 10.356484], notes: "Scheduled:", ip: "XHgwMmB7fk1lRn1Bg8ooqhwM4xQsg=", notify_email: "", notify_phone: "", user_id: "53f4b08", thingy_type: "Blue Row", thingy_request_id: nil, error: nil, two_hour_warning_sent: nil, identifier: nil, estimated_time: "", thingy_request_log: nil, for_customer_api: false, removed: false, edit_date: "2018-08-27", edit_hours: "08", edit_minutes: "00", edit_ampm: "PM">

    ];

更新:

我剝離了數據並創建了一個新的Ruby數組。

result = []
sr.each do |r|
  record = {}
  record[:send_time] = r.send_time
  record[:ambulance_type] = r.ambulance_type
  result << {:ambulance => record}
end
result

我現在正在用這個。 var taken = <%= @taken.as_json %>;

現在得到這個(擦洗數據以獲得敏感信息)。

var taken = [{&quot;thingy&quot;=&gt;{&quot;send_time&quot;=&gt;&quot;2018-08-25T18:00:00.000+00:00&quot;, &quot;thingy_type&quot;=&gt;&quot;Row Delivery&quot;}}

是的,如您所見, @taken.inspect.html_safe並不是將您的activerecord關系傳遞給JS的好方法。 如果將其作為json傳遞,可能會更有意義:

var taken = <%= @taken.as_json %>

但是請記住,用這種方式可以將所有記錄的內容公開到前端,這可能是不希望的。

首先,請注意as_jsonto_json之間的區別。 as_json返回哈希,然后將其字符串化。 to_json實際上會創建一個JSON字符串。 有關更多詳細信息和參考,請參見此問題: Ruby中的as_json和to_json方法之間的區別

我建議您更新ScheduledRequest模型,使其具有as_json方法,該方法可以有效地完成each更新中的操作(返回單個ScheduledRequest的哈希表示。然后,在您的視圖中,您可以

var taken = <%= @taken.to_json.html_safe %>

to_json將返回一個JSON數組,而html_safe將確保它不會得到html編碼(這就是您在上面看到的內容)。 拉扯有關如何表示單個ScheduledRequest對象的邏輯,可以確保該類負責確定如何將自己表示為JSON,而不必擔心視圖。 需要注意的是,當你擁有它,你正在你的@taken在JavaScript變量訪問如taken通過上面的分配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM