Rails ActiveRecord to_json留下无效的json字符

时间:2017-10-26 17:59:07

标签: javascript ruby-on-rails json

我正在尝试将rails ActiveRecord转换为json,并在尝试解析数据时不断收到无效字符。

@appts = current_cbu.appointments.on_date(@date).as_json.to_json

我已经覆盖了'as_json方法,如下所示:

  def as_json
    hash = {}
    hash["title"] = calendar_title
    hash["start"] = start_at.strftime("%Y-%m-%d")
    hash["end"] = end_at.strftime("%Y-%m-%d")
    hash["url"] = "/admin/appointments/#{id}"
    hash["base_amount"] = base_amount
    hash["upcharge_amount"] = upcharge_amount
    hash["upcharge_reason"] = upcharge_reason
    hash["total_with_tip_amount"] = total_with_tip_amount
    hash["customer_notes"] = customer_notes
    hash
  end

然后在我的视图中,我将其发布到一个单独的应用程序,但在单独的应用程序上,当我尝试执行JSON.parse()时,我会收到错误。如何将@appts转换为有效的json而不必为每个无效的字符执行.gsub?

 var post_data = {
    'type': 'initial',
    'appts': '#{@appts}',
    'assignments': '#{@daily_assignments_json}',
    'events': '#{@events_json}',
    'date' : '#{@date}'
  }
  targetWindow.postMessage(JSON.stringify(post_data), '#{@scheduler_location}');

错误:VM30212:1未捕获的SyntaxError:意外的令牌  在位置663的JSON中

0 个答案:

没有答案