Ember CLI Rails: 创建用户模型
Ember CLI Rails: Creating user model
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) 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代码
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)
在意识到 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
使用 rails 提供的 .full_messages 后,返回的 json 响应采用以下格式。
{ errors: ["Password can't be blank", "Name can't be blank"] }
希望这对其他人有所帮助,如果需要,我们很乐意提供更多!
我的情况以防对任何人有帮助:我正在将我的纯 rails 应用程序转换为具有 ember 前端的 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
s 用于新域,但得到 422 无法处理的实体并且必须刷新页面以反映更改(新域)。 完全删除 if/else
语句后,我的 ember 应用运行得非常好。
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) 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代码
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)
在意识到 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
使用 rails 提供的 .full_messages 后,返回的 json 响应采用以下格式。
{ errors: ["Password can't be blank", "Name can't be blank"] }
希望这对其他人有所帮助,如果需要,我们很乐意提供更多!
我的情况以防对任何人有帮助:我正在将我的纯 rails 应用程序转换为具有 ember 前端的 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
s 用于新域,但得到 422 无法处理的实体并且必须刷新页面以反映更改(新域)。 完全删除 if/else
语句后,我的 ember 应用运行得非常好。