Last active
December 17, 2015 20:46
-
-
Save amaanr/5577152 to your computer and use it in GitHub Desktop.
Using Rails & Devise with Ember.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.User = DS.Model.extend | |
email: DS.attr('string') | |
name: DS.attr('string') | |
password: DS.attr('string') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSerializer < ActiveModel::Serializer | |
attributes :id, :name, :email | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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