0
votes

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"=>"[email protected]", "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" = '[email protected]' LIMIT 1
     (0.1ms)  ROLLBACK
  Completed 422 Unprocessable Entity in 118ms (Views: 0.9ms | ActiveRecord: 0.7ms)
2
Post your request data from browser developers tools (or from rails server logs). Maybe it will help somehow.Kuba Niechciał
So I've realized, the 422 is the response you only get when validation fails. However I can't seem to access my errors in order to stop save and send back error handling styles. I've tried manipulating json response but no luck.arinh

2 Answers

0
votes

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!

0
votes

My situation in case it helps anyone: I was in the process of converting my purely rails app to rails with an ember frontend. In my case, I was saving an @domain (not an @user like the OP, but same principle applies).

My domains_controller.rb had this if/else statement (which was useful in rubyland):

def create
  @user = current_user
  @domain = current_user.domains.build(app_params)
  @new_domain = Domain.new
  if @domain.save
    flash.now[:notice] = "Domain registered."
  else
    flash[:error] = "There was an error registering the domain. Try again."
  end
  respond_with current_user.domains.create(app_params)
end

But these flash notices were not useful in emberland (which handles its error messages in its own way). I was able to createRecords for new domains, but was getting 422 unprocessable entity and would have to refresh the page to reflect the change (new domain). Once I completely removed the if/else statement, my ember app worked beautifully.