2015-05-25 60 views
0

灰燼(0.2.5),導軌(4.2),灰燼CLI軌,設計,灰燼CLI簡單驗證設計灰燼CLI的Rails:創建用戶模型

行動創建在用戶模式 ​​'用戶/新'控制器。目前,Ruby代碼在數據庫中創建用戶,但JavaScript響應仍然失敗。在使用用戶模型創建的Ember CLI和Rails中,我一直無法找到很多資源。這裏是我迄今使用的資源(https://adibsaad.com/blog/setting-up-ember-cli-with-a-rails-back-end-and-token-authentication-authorization)和(http://kbarrett.com/blog/2014/03/24/authentication-with-ember-rails.html)。

POST http://localhost:3000/users 422(無法處理的實體)

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代碼

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 

讓我知道如果我需要提供其他任何東西。

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) 
+1

從瀏覽器開發者工具(或從rails服務器日誌)發佈您的請求數據。也許它會幫助某種方式。 –

+0

所以我意識到,422是你在驗證失敗時纔會得到的迴應。然而,我似乎無法訪問我的錯誤,以停止保存併發回錯誤處理樣式。我試過操縱JSON響應,但沒有運氣。 – arinh

回答

0

體會什麼422響應實際上意味着什麼,我需要傳遞迴我結束了對發送JSON錯誤早在user_controller.rb和顯示咆哮爲每一個後。

JS控制器動作:

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 

JSON響應回來是利用軌道提供.full_messages後的格式如下。

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

希望這可以幫助別人,並樂意提供更多,如果需要!

0

我的情況,如果它有助於任何人:我正在將我純粹的rails應用程序轉換爲帶有前端的rails。在我的情況下,我保存了@domain(不是像OP一樣的@user,但是同樣的原則適用)。

domains_controller.rb有這個if/else語句(這是在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 

但這些閃光聲明中並未在emberland有用的(它處理自己的方式它的錯誤消息)。我能夠爲新域名createRecord s,但得到422無法處理的實體,並且必須刷新頁面以反映更改(新域)。 一旦我完全刪除if/else聲明,我的燼應用程序工作得很漂亮。