Ember(0.2.5),Rails(4.2),Ember CLI Rails,Devise,Ember CLI Simple Auth Devise
在' user / new'上创建用户模型的操作控制器。目前,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 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
如果我需要提供其他任何内容,请告诉我。
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)
答案 0 :(得分:0)
在意识到422响应实际意味着什么以及我需要传回的内容后,我总结了在user_controller.rb中发送json错误并为每一个显示咆哮。
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
使用rails提供的.full_messages后,json响应回复采用以下格式。
{ errors: ["Password can't be blank", "Name can't be blank"] }
希望这有助于其他人,并且如果需要,很乐意提供更多!
答案 1 :(得分: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
但是这些flash通知在emberland中没有用(它以自己的方式处理错误消息)。我能够createRecord
用于新域,但是获得了422个不可处理的实体,并且必须刷新页面以反映更改(新域)。 一旦我完全删除了if/else
语句,我的余烬应用程序就能很好地运行。