0

I have encountered a problem in my ruby project. What I am trying to do is just to modify email addresses and update them. However, the emails are just not updated. I also did not get any error messages. I find the debugging process very difficult with angularJS and rails. So, I am desperately looking for help in here.

Personally, I am thinking that the problem might be with the routing or the "name: user" in the factory. Please assist me in finding my mistakes. Thank you very much.

This is the manage_users_controller.rb

def edit
  if request.format.json?
   t = @user
   @user = {:id => t.id, :email => t.email}
  end
  respond_to do |format|
    format.json { render :json => @user, :status => 200}
    format.html
  end
 end

 def update
  @user.attributes = users_params
  if @user.save
   respond_to do |format|
    format.json {render :json => {:redirect_to => admin_manage_users_path }, :status => 200 }
   end
  else
   respond_to do |format|
    format.json { render :json => {:errMsg => print_out_message('form-update','user'), :error => @user.errors}, :status => 400 } 
   end
  end
 end

 def users_params
  params.require(:user).permit(:id, :email)
 end

and the edit.html.erb

<div class="row" ng-controller="AdminManageUsersCtrl">
 <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
  <div class="rs-content" data-ng-init="loadUser()">
   <div class="portlet listing" cg-busy="loadUser" ng-cloak>
     <div class="portlet-body">
      <form name="form" ng-submit="uploadUser()" confirm-on-exit>
       <%= render "admin/manage_users/form" %>
       <div class="row row-space-top-1">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
         <button type="submit" class="btn red-mint" ng-click="form.$setPristine()">Update</button>
         <%= link_to "Cancel".html_safe, admin_manage_users_path, :class => "btn dark btn-outline" %>
         <%= link_to 'Remove', URI::unescape(admin_manage_user_path(:users => '{{user.id}}')), :method => :delete, :class => "pull-right btn red btn-outline", :data => {:confirm => 'Are you sure you want to delete this user?', "ng-click"=>"form.$setPristine()"} %>
        </div>
       </div>
      </form>
     </div>
   </div>
  </div>
 </div>
</div>

and app.js excerpt

app.factory('AdminManageUsers', ['railsResourceFactory', 'railsSerializer', function(railsResourceFactory, railsSerializer) {
 return railsResourceFactory({
  url: '/user',
  name: 'user'
 });
}]);


$scope.uploadUser = function() {
  $scope.errors = [];
  AdminManageUsers.$patch('update', $scope.user).then(function(response) {
   $window.location.href = response.redirectTo;
  }, function(response) {
   angular.forEach(response.data.errors, function(errors, key) {
    $scope['form'][key].$invalid = true;
    $scope.errors[key] = errors.join(', ');
   });
   alertMessage.danger(response.data.errMsg, 1, 10000);
  });
 }

and the routes.rb

resources :manage_users, :only => [:index,:destroy], :param => :id# <== generate new manage_user path
   match "manage_users/data" => "manage_users#data", via: [:post], :defaults => { :format => 'json' }
   match "manage_users/load" => "manage_users#load", via: [:get], :defaults => { :format => 'json' }
   match "manage_users/:id/edit" => "manage_users#edit", via: [:get]
   match "manage_users/:id/update" => "manage_users#update", via: [:patch]

2 Answers 2

1

Ok, I found out my mistake. It was because of the device authentication. When I update, devise will not allow update of email, so the new email will be stored as unconfirmed email. So I have been looking at the wrong place for solution. It wasn't with the routing etc, but it was with devise :(

To let device allow change of email, I changed the update method in manage_user_controller.rb:

 def update
  @user.skip_reconfirmation!
  if @user.update_attributes(users_params)
    respond_to do |format|
    format.json {render :json => {:redirect_to => admin_manage_users_path }, :status => 200 }
   end
Sign up to request clarification or add additional context in comments.

Comments

0

your AdminManageUsersseem to point to /user but your rails resource namespace is /manage_users, try to change this in your angular factory and see if this works

1 Comment

I changed user to manage_users in both url and name in the factory, but update still does not work correctly. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.