I have a simple rails 3.2 app with a Backbone.js front-end. I have the following before_filter
and helper defined on my back-end:
# my before_filter placed in most controllers
def require_user
unless logged_in?
redirect_to '/#login'
end
end
def logged_in?
!!current_user
end
def current_user
@_current_user ||= session[:user_id] && Appuser.find_by_id(session[:user_id])
end
This works great for restricting access to most actions to logged in users. However, I am also bootstrapping data into my Backbone.js collections with a <script>
tag at the bottom of my main application template (haml):
!!!
%html{:lang => "en"}
%head
%title InfoUser
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%script
$(document).ready(function(){ App.init(); });
%body
-# Layout markup
:javascript
projects.reset(#{Project.all.to_json})
groups.reset(#{Group.all.to_json})
users.reset(#{User.all.to_json})
= render "layouts/templates"
The problem is, even if an unauthenticated user access the app and is immediately redirected to the login page, this bootstrapping script still gets added to the bottom of the page, and all the data shows up in the collections.
Is there a way in haml to check the status of the user, or is there another way I should handle bootstrapping that waits until the user is authenticated?