Ember CLI Rails:创建用户模型
Ember CLI Rails: Creating user model
Ember (0.2.5), Rails (4.2), Ember CLI Rails, design, Ember CLI Simple Auth design
在'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 (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 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
使用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应用程序工作得很漂亮。
- 如何在Meteor中扩展用户模型
- 如何调用索引/索引路由'用户后的s模型挂钩's的授权
- 与设计用户相关联的多个模型-根据角色选择填写一个模型
- 有没有任何方法可以检测到用户'的Mac硬件模型
- 将用户键入的模型更改为自定义的Angular指令
- 使用 Mocha 在 NodeJS 和 MongoDB 中测试用户模型时没有结果
- 检测谁更改了模型(用户输入与控制器)angularjs
- MVC 5 Razor 中的引导模式:当用户单击模式中的继续按钮时,如何将模型布尔值更新为 false
- 环回 + 用户模型迁移到 Postgres 数据库
- 如何使用 node.js 创建用户模型
- 如何检测用户是否在IE中设置主页模型窗口中单击“是或否”
- 具有多个更新路径的主干用户模型
- 在Express中使用Mongoose将一个模型引用到一个用户模型
- 使用用户模型环回时出现401错误
- 我如何正确使用MongoDB的Strongloop脚手架用户模型存储
- 在骨干js中处理登录的用户模型和视图
- Azure移动服务-在插入脚本上更改用户模型
- 为RESTful JSON API构建一个Backbone.js用户模型、集合和视图
- 如何防止整个用户模型泄漏到jade模板中
- Ember CLI Rails:创建用户模型