Search code examples
javascriptruby-on-rails-4ember.jsdeviseember-cli

Ember CLI Rails: Creating user model


Ember (0.2.5), Rails (4.2), Ember CLI Rails, Devise, Ember CLI Simple Auth Devise

Action for creating the user model on the 'user/new' controller. Currently the ruby code creates the user in the database however the javascript response is still failing. I haven't been able to find much resources on Ember CLI and Rails with user model creation. Here are the resources I have been using so far (https://adibsaad.com/blog/setting-up-ember-cli-with-a-rails-back-end-and-token-authentication-authorization) and (http://kbarrett.com/blog/2014/03/24/authentication-with-ember-rails.html).

POST http://localhost:3000/users 422 (Unprocessable Entity)

actions: {
createUser: function() {
  var user = this.store.createRecord('user', {
    name: this.get('name'),
    gradePoints: this.get('gradePoints'),
    gradeUnits: this.get('gradeUnits'),
    email: this.get('email'),
    password: this.get('password')
  });
  var self = this;
  user.save().then(function() {
    self.get('session').authenticate('ember-simple-auth-authenticator:devise', {
      identification: user.get('email'),
      password: user.get('password')
    }).then((function() {
      self.transitionToRoute('dashboard');
    }));
  }, function() {
    alert('Failed to register user.');
  });
 }
}

Ruby Code

class UsersController < ApplicationController
  respond_to :json

  def create
    user = User.new(user_params)

    binding.pry
    if user.save
      render json: user, status: :created
    else
      respond_with user
    end
  end

  private

  def user_params
    params.require(:user).permit(:name, :email, :gradePoints, :gradeUnits, :password, :password_confirmation)
  end

end

Let me know if I need to provide anything else.

  Started POST "/users" for ::1 at 2015-05-25 09:36:54 -0700
  Processing by UsersController#create as JSON
    Parameters: {"user"=>{"email"=>"user@example.com", "name"=>"Arin", "gradePoints"=>217.665, "gradeUnits"=>63, "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
     (0.2ms)  BEGIN
    User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = 'user@example.com' LIMIT 1
     (0.1ms)  ROLLBACK
  Completed 422 Unprocessable Entity in 118ms (Views: 0.9ms | ActiveRecord: 0.7ms)

Solution

  • After realizing what 422 response actually meant and what I needed to pass back I concluded on sending the json errors back in user_controller.rb and displaying growls for each one.

    JS Controller Action:

        actions: {
          createUser: function() {
            var user = this.store.createRecord('user', this.get('model'));
            var self = this;
            user.save().then(function() {
              self.get('session').authenticate('simple-auth-authenticator:devise', {
                identification: user.get('email'),
                password: user.get('password')
              }).then((function() {
                self.transitionToRoute('dashboard');
                $.growl.notice({ message: "You have successfully registered." });
              }));
            }, function(response) {
              user.rollback();
              var errors = response.responseJSON.errors;
              errors.forEach(function(error_message){
                $.growl.error({ message: error_message });
              });
            });
          }
        }
    

    user_controller.rb

      class UsersController < ApplicationController
        respond_to :json
    
        def create
          @user = User.new(user_params)
    
          if @user.save
            render json: @user, status: :created
          else
            render json: { errors: @user.errors.full_messages }, status: :unprocessable_entity
          end
        end
    
        private
    
        def user_params
          params.require(:user).permit(:name, :email, :gradePoints, :gradeUnits, :password, :password_confirmation)
        end
    
      end
    

    The json response came back was in the following format after using .full_messages provided by rails.

    { errors: ["Password can't be blank", "Name can't be blank"] }
    

    Hope this helps someone else, and gladly provide more if needed!