Skip to content

Instantly share code, notes, and snippets.

@amaanr
Last active December 17, 2015 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amaanr/5577152 to your computer and use it in GitHub Desktop.
Save amaanr/5577152 to your computer and use it in GitHub Desktop.
Using Rails & Devise with Ember.js
<!DOCTYPE html>
<html>
<head>
<title>App</title>
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
<%= csrf_meta_tags %>
<%- if user_signed_in? %>
<meta name="current-user" content="<%= UserSerializer.new(current_user).to_json(root: false) %>" />
<% end %>
</head>
<body>
<div class="container">
<%= yield %>
</div>
</body>
</html>
Ember.Application.initializer
name: 'currentUser'
initialize: (container) ->
store = container.lookup('store:main')
attributes = $('meta[name="current-user"]').attr('content')
console.log attributes
if attributes
object = store.load(App.User, JSON.parse(attributes))
user = App.User.find(object.id)
controller = container.lookup('controller:currentUser').set('content', user)
container.typeInjection('controller', 'currentUser', 'controller:currentUser')
<header>
<h1>
{{#if currentUser.isSignedIn}}
Hi, {{currentUser.name}}
{{else}}
Hello, Guest
{{/if}}
</h1>
</header>
App.Router.map(function() {
this.resource("users", {path: '/users'}, function(){
this.route('new');
this.route('show', {path: '/:user_id'}) ;
this.route('edit', {path: '/:user_id/edit'});
});
this.route('home');
});
App.IndexRoute = Ember.Route.extend({
redirect: function(){
this.transitionTo ('home');
}
})
App.User = DS.Model.extend
email: DS.attr('string')
name: DS.attr('string')
password: DS.attr('string')
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :email
end
class UsersController < ApplicationController
before_filter :authenticate_user!
def home
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment