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]