{"id":1175,"date":"2015-08-05T13:59:52","date_gmt":"2015-08-05T13:59:52","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=1175"},"modified":"2015-08-05T13:59:52","modified_gmt":"2015-08-05T13:59:52","slug":"status-feed-with-angularjs","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/status-feed-with-angularjs\/","title":{"rendered":"Ruby on Rails Tutorial status feed with AngularJS"},"content":{"rendered":"<p><strong>1)Rails controller<\/strong><\/p>\r\n\r\n<p>$ vi app\/controllers\/sessions_controller.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\n  def current_user\r\n    remember_token = User.encrypt(cookies[:remember_token])\r\n    current_user ||= User.find_by(remember_token: remember_token)\r\n    if current_user\r\n      gravatar_id = Digest::MD5::hexdigest(current_user.email.downcase)\r\n      conn = ActiveRecord::Base.connection\r\n      sql = &quot;SELECT microposts.*,users.name,users.email &quot; +\r\n            &quot;FROM microposts,users &quot; +\r\n            &quot;WHERE (user_id IN (&quot; +\r\n            &quot;SELECT followed_id FROM relationships WHERE follower_id = &quot; +\r\n            current_user.id.to_s + &quot; )&quot; +\r\n            &quot;OR user_id = &quot; + current_user.id.to_s + &quot; ) &quot; +\r\n            &quot;AND microposts.user_id = users.id&quot;\r\n      feed = conn.select_all(sql)\r\n      @user_info = {\r\n        user: current_user,\r\n        gravatar_url: &quot;https:\/\/secure.gravatar.com\/avatar\/#{gravatar_id}&quot;,\r\n        microposts: current_user.microposts,\r\n        feed: feed,\r\n        followed_users: current_user.followed_users,\r\n        followers:  current_user.followers\r\n      }\r\n      render json: @user_info, status: :accepted\r\n    else\r\n      head :no_content\r\n    end\r\n  end<\/pre>\r\n\r\n<p><strong>2)AngularJS template view<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/templates\/static_pages\/home.html.erb<\/p>\r\n\r\n<div ng-non-bindable><pre class=\"prettyprint\">\r\n&lt;h3&gt;Micropost Feed&lt;\/h3&gt;\r\n&lt;ol class=&quot;microposts&quot;&gt;\r\n  &lt;li ng-repeat=&quot;mp in currentitems&quot;&gt;\r\n    &lt;a href=&quot;\/users\/{{mp.user_id}}&quot;&gt;\r\n      &lt;img alt=&quot;{{mp.name}}&quot;\r\n           src=&quot;https:\/\/secure.gravatar.com\/avatar\/{{hash(mp.email)}}?s=60&quot;\r\n           class=&quot;gravatar&quot;\r\n      \/&gt;\r\n    &lt;\/a&gt;\r\n    &lt;a href=&quot;\/users\/{{mp.user_id}}&quot;&gt;{{mp.name}}&lt;\/a&gt;\r\n    &lt;div&gt;{{mp.content}}&lt;\/div&gt;\r\n    &lt;small class=&quot;text-muted&quot;&gt;Posted {{mp.created_at | date:&#39;medium&#39;}}&lt;\/small&gt;\r\n  &lt;\/li&gt;\r\n&lt;\/ol&gt;\r\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>1)Rails controller $ vi app\/controllers\/sessions_controller.rb 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) conn = ActiveRecord::Base.connection sql = &quot;SELECT microposts.*,users.name,users.email &quot; + &quot;FROM microposts,users &quot; + &quot;WHERE (user_id IN (&quot; + &quot;SELECT followed_id FROM relationships WHERE follower_id = &quot; + current_user.id.to_s + &quot; )&quot; + &quot;OR user_id = &quot; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[191,192,190],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1175"}],"collection":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/comments?post=1175"}],"version-history":[{"count":2,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1175\/revisions"}],"predecessor-version":[{"id":1311,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1175\/revisions\/1311"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=1175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=1175"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=1175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}