从Backbone检索rails / devise current_user

我有一个应用程序,我用Rails通过Devise管理注册/进/出。

当我登录时,我被重定向到Backbone启动的Dashboard #index。

我想以某种方式检索Backbone中的current_user.id和current_user.token。

我唯一的想法是在我的dashboard.html.haml中有这个

:javascript window.App.current_user.id = "#{current_user.id}" window.App.current_user.token = "#{current_user.token}" 

(请注意,客户端只能访问公共信息)

要获得更优雅的解决方案,您可以创建UserSession Backbone模型,并将其作为正常的Backbone模型从服务器fetch

 window.App.UserSession = Backbone.Model.extend({ urlRoot: "/session" }); 

如果不想发出此额外请求,您可以使用模板呈现时间中已有的数据显式加载模型:

 window.App.current_user = new window.App.UserSession({ id: "#{current_user.id}", token: "#{current_user.token}" });