{"id":1127,"date":"2015-08-05T01:53:51","date_gmt":"2015-08-05T01:53:51","guid":{"rendered":"http:\/\/www.honobono-life.info\/wpeng\/?p=1127"},"modified":"2015-08-05T01:55:22","modified_gmt":"2015-08-05T01:55:22","slug":"delete-user-with-angularjs","status":"publish","type":"post","link":"http:\/\/www.honobono-life.info\/wpeng\/delete-user-with-angularjs\/","title":{"rendered":"Ruby on Rails Tutorial delete user with AngularJS"},"content":{"rendered":"<p><strong>(1)Administrative users<\/strong><\/p>\r\n\r\n<p><strong>1)adds the admin column to the users table<\/strong><\/p>\r\n\r\n<p>$ rails generate migration add_admin_to_users admin:boolean<\/p>\r\n\r\n<p>$ vi db\/migrate\/&#8230;._add_admin_to_users.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nclass AddAdminToUsers &lt; ActiveRecord::Migration\r\n  def change\r\n    add_column :users, :admin, :boolean, default: false\r\n  end\r\nend<\/pre>\r\n\r\n<p><strong>2\uff09migrate<\/strong><\/p>\r\n\r\n<p>$ bundle exec rake db:migrate<\/p>\r\n\r\n<p><strong>3\uff09sample data<\/strong><\/p>\r\n\r\n<p>$ rails console<\/p>\r\n\r\n<p>2.0.0p247 :001 &gt; adminuser = User.create(name: &quot;admin&quot;, email: &quot;admin@example.com&quot;, password: &quot;xxxxx&quot;, password_confirmation: &quot;xxxxx&quot;,admin: true)<br \/>\r\n2.0.0p247 :002 &gt; adminuser.admin?<br \/>\r\n&nbsp;=&gt; true<\/p>\r\n\r\n<p><strong>(2)Rails controller<\/strong><\/p>\r\n\r\n<p><strong>1)destroy action<\/strong><\/p>\r\n\r\n<p>$ vi app\/controllers\/users_controller.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\n  def destroy\r\n    User.find(params[:id]).destroy\r\n    head :no_content\r\n  end<\/pre>\r\n\r\n<p><strong>2)users have to be logged in to delete users<\/strong><\/p>\r\n\r\n<p>$ vi app\/controllers\/users_controller.rb<\/p>\r\n\r\n<p>before_action :signed_in_user, only: [:index,:update,:destroy]<\/p>\r\n\r\n<p><strong>3)only admins can delete users<\/strong><\/p>\r\n\r\n<p>$ vi app\/controllers\/users_controller.rb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\n  before_action :admin_user,     only: :destroy\r\n......\r\n  private\r\n........\r\n    def admin_user\r\n      remember_token = User.encrypt(cookies[:remember_token])\r\n      current_user ||= User.find_by(remember_token: remember_token)\r\n      render status: :unauthorized unless current_user.admin?\r\n    end<\/pre>\r\n\r\n<p><strong>(3)AngularJS template view<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/templates\/users\/index.html.erb<\/p>\r\n\r\n<div ng-non-bindable><pre class=\"prettyprint\">\r\n&lt;ul ng-repeat=&quot;user in currentitems&quot; class=&quot;users&quot;&gt;\r\n  &lt;li&gt;\r\n    &lt;img alt=&quot;{{user.name}}&quot; src=&quot;https:\/\/secure.gravatar.com\/avatar\/{{hash(user.email)}}?s=52&quot; \/&gt;\r\n    &lt;a href=&quot;\/users\/{{user.id}}&quot;&gt;{{user.name}}&lt;\/a&gt;\r\n    &lt;a ng-show=&quot;{{ychkAdmin}}&quot; href=&quot;&quot; ng-click=&quot;delete(user.id)&quot;&gt;| delete&lt;\/a&gt;\r\n  &lt;\/li&gt;\r\n&lt;\/ul&gt;<\/pre><\/div>\r\n\r\n<p><strong>(4)AngularJS controller<\/strong><\/p>\r\n\r\n<p>$ vi app\/assets\/javascripts\/mymodule.js.erb<\/p>\r\n\r\n<pre class=\"prettyprint\">\r\nmyModule.controller(&quot;UsersIndexCtrl&quot;, function($scope, userResource, flashService, $location, sessionResource, $q) {\r\n......\r\n  var deferred = $q.defer();\r\n  deferred.promise.then(function (result) {\r\n    var user_info = result;\r\n    if (user_info.user.id &gt; 0) {\r\n      $scope.chkAdmin = user_info.user.admin;\r\n      userResource.index({}, function(response) {\r\n        $scope.users = response;\r\n        $scope.totalitems = $scope.users.length;\r\n        $scope.currentitems = $scope.users.slice(start,end);\r\n\r\n        $scope.$watch(&#39;currentpage&#39;, function() {\r\n          start = ($scope.currentpage - 1) * $scope.itemsperpage;\r\n          end = start + $scope.itemsperpage;\r\n          $scope.currentitems = $scope.users.slice(start,end);\r\n        },true);\r\n      });\r\n    } else {\r\n      $location.path(&quot;\/signin&quot;);\r\n    }\r\n  },function (reason) {\r\n    console.log(&quot;qgetUser-Error&quot;);\r\n  })\r\n  qgetUser (deferred);\r\n......\r\n  $scope.delete = function(id) {\r\n    userResource.destroy({ id: id });\r\n    userResource.index({}, function(response) {\r\n      $scope.users = response;\r\n      $scope.totalitems = $scope.users.length;\r\n      $scope.currentitems = $scope.users.slice(start,end);\r\n    });\r\n  };\r\n});\r\n<\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>(1)Administrative users 1)adds the admin column to the users table $ rails generate migration add_admin_to_users admin:boolean $ vi db\/migrate\/&#8230;._add_admin_to_users.rb class AddAdminToUsers &lt; ActiveRecord::Migration def change add_column :users, :admin, :boolean, default: false end end 2\uff09migrate $ bundle exec rake db:migrate 3\uff09sample data $ rails console 2.0.0p247 :001 &gt; adminuser = User.create(name: &quot;admin&quot;, email: &quot;admin@example.com&quot;, password: &quot;xxxxx&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":[175,35,177,176],"amp_enabled":true,"_links":{"self":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1127"}],"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=1127"}],"version-history":[{"count":1,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1127\/revisions"}],"predecessor-version":[{"id":1128,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/posts\/1127\/revisions\/1128"}],"wp:attachment":[{"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/media?parent=1127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/categories?post=1127"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.honobono-life.info\/wpeng\/wp-json\/wp\/v2\/tags?post=1127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}