Web Programming examples

Google Maps,AngularJS

Ruby on Rails Tutorial add follower stats to the profile page

1)Rails controller

$ vi app/controllers/users_controller.rb

  def show
    user = User.find(params[:id])
    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,
      followed_users: user.followed_users,
      followers:  user.followers
    }
    render json: @user_info
  end

2)AngularJS template view

$ vi app/assets/templates/users/show.html.erb

<div class="col-xs-6 col-sm-4">
  <h3>
    <img alt="{{user_info.user.name}}" src="{{user_info.gravatar_url}}" />
      {{user_info.user.name}}
  </h3>
  <div class="stats">
    <a href="#">
      <strong id="following" class="stat">
        {{user_info.followed_users.length}}
      </strong>
      following
    </a>
    <a href="#">
      <strong id="followers" class="stat">
        {{user_info.followers.length}}
      </strong>
      followers
    </a>
  </div>
</div>