In my view I want to create a javascript object that matches my ruby object current_user
Seems like a good solution instead of making an extra request to /users/me.
This took me longer than it should have:
<script>
window.current_user = <%= raw(current_user.to_json) %>;
</script>
send json from controller to view
def show_user_categories
@categories= current_users.categories.map do |c|
{ :id => c.id,:name => c.title}
end
respond_to do |format|
format.html
format.json { render :json => @categories}
end
end
fetch the json in view
<%= javascript_tag do%>
window.categories= <%=raw @categories.to_json %>;
<%end%>
and then use it in js script
var markers=[];
$.each(categories, function(index) {
//creating js array which can be used in js script
//using the id/title passed from controller code
markers.push([categories[index]['id'],categories[index]['title']]);
});