Web Programming examples

Google Maps,AngularJS

Archives

Ruby on Rails Tutorial adding microposts count and gravatar to home page with AngularJS

1)Rails controller

$ vi app/controllers/sessions_controller.rb

  def create
    user = User.find_by(email: session_params[:email].downcase)
    if user && user.authenticate(session_params[:password])
      remember_token = User.new_remember_token
      cookies.permanent[:remember_token] = remember_token
      user.update_attribute(:remember_token, User.encrypt(remember_token))
      gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
      microposts = user.microposts
      @user_info = {
        user: user,
        gravatar_url: "https://secure.gravatar.com/avatar/#{gravatar_id}",
        microposts: microposts
      }
      render json: @user_info, status: :accepted, location: user
    else
      msg = {"password" => ["Invalid email/password combination"]}
      render json: msg, status: :unprocessable_entity
    end
  end

  def current_user
    remember_token = User.encrypt(cookies[:remember_token])
    current_user ||= User.find_by(remember_token: remember_token)
    if current_user
      gravatar_id = Digest::MD5::hexdigest(current_user.email.downcase)
      microposts = current_user.microposts
      @user_info = {
        user: current_user,
        gravatar_url: "https://secure.gravatar.com/avatar/#{gravatar_id}",
        microposts: microposts
      }
      render json: @user_info, status: :accepted
    else
      head :no_content
    end
  end

2)AngularJS "UsersNewCtrl" controller

myModule.controller("UsersNewCtrl", function($scope, userResource, $location, flashService, $routeParams, sessionResource, $q) {
.....
  $scope.submit = function() {
    function success(response) {
.........
      var user_info = { 
        user: response,
        gravatar_url: "https://secure.gravatar.com/avatar/" + md5(response.email.toLowerCase())
      };
      flashService.setUser(user_info);
      $location.path("/users/" + response.id);
    }

3)AngularJS "HomeCtrl" controller and template view

1.AngularJS "HomeCtrl" controller

$ vi app/assets/javascripts/mymodule.js.erb

myModule.controller("HomeCtrl", function($scope,flashService) {
  $scope.chkSignin = function() {
    return flashService.getUser();
  };
});

2.AngularJS template view

$ vi app/assets/templates/static_pages/home.html.erb

<div ng-controller="HomeCtrl">
  <div ng-show="chkSignin().user.id > 0" class="row">
    <div class="col-sm-4">
      <a href="/users/{{chkSignin().user.id}}">
        <img alt="{{chkSignin().user.name}}"
             src="{{chkSignin().gravatar_url}}?s=60"
             class="gravatar"
        />
      </a>
      <h4>{{chkSignin().user.name}}</h4>
      <span><a href="/users/{{chkSignin().user.id}}">view my profile</a></span><br />
      <ng-pluralize count="chkSignin().microposts.length"
                    when="{'0': '0 micropost',
                           'one': '1 micropost',
                           'other': '{} microposts'}">
      </ng-pluralize>
    </div>
  </div>
  <div ng-hide="chkSignin().user.id > 0">
    <div class="jumbotron">
      <h1>Welcome to the Sample App</h1>
.........