Extract rails / create current_user from Backbone

I have an application in which I control registration / input / output using Rails through Devise.

When I logged in, I was redirected to the Dashboard # index, where Backbone runs.

I would like to somehow restore my current_user.id and current_user.token to Backbone.

My only idea is to have this in my dashboard.html.haml

:javascript window.App.current_user.id = "#{current_user.id}" window.App.current_user.token = "#{current_user.token}" 
+6
source share
1 answer

(Beware that only publicly available information should be available on the client side)

For a more elegant solution, you can create a UserSession Backbone and fetch model from the server as a regular Backbone model.

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

If you do not want to make this additional request, you can explicitly load the model with the data already available in the process of rendering the template:

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

Source: https://habr.com/ru/post/910067/


All Articles