以下是我目前使用的版本:
Ember方面的行为就像更新成功,但新的API请求带来了一个未经改动的用户......下面是Rails的输出:
22:07:18 web.1 | Processing by Api::V1::UsersController#update as JSON
22:07:18 web.1 | Parameters: {"user"=>{"full_name"=>"Sample User hghghfg", "display_name"=>"User#1", "password"=>"[FILTERED]", "email"=>"user@example.com", "date_of_birth"=>"2009-09-14T00:00:00.000Z", "gender"=>"male", "facebook_url"=>"", "twitter_url"=>"https://twitter.com/Deovandski", "personal_message"=>"sample personal message", "webpage_url"=>"www.google.com", "is_banned"=>false, "is_banned_date"=>nil, "legal_terms_read"=>true, "privacy_terms_read"=>true, "is_admin"=>true, "is_super_user"=>true, "sign_in_count"=>634, "last_sign_in_at"=>"2015-08-11T02:07:11.600Z", "updated_at"=>"2015-08-11T02:07:11.723Z", "created_at"=>"2015-08-10T03:16:28.314Z", "show_full_name"=>true}, "id"=>"2"}
22:07:18 web.1 | User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."email" = ? LIMIT 1 [["email", "user@example.com"]]
22:07:18 web.1 | (0.1ms) begin transaction
22:07:18 web.1 | SQL (0.2ms) UPDATE "users" SET "last_sign_in_at" = ?, "current_sign_in_at" = ?, "sign_in_count" = ?, "updated_at" = ? WHERE "users"."id" = ? [["last_sign_in_at", "2015-08-11 02:07:11.875566"], ["current_sign_in_at", "2015-08-11 02:07:18.560464"], ["sign_in_count", 636], ["updated_at", "2015-08-11 02:07:18.563145"], ["id", 2]]
22:07:18 web.1 | (22.1ms) commit transaction
22:07:18 web.1 | User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 2]]
22:07:18 web.1 | CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "2"]]
22:07:18 web.1 | (0.1ms) begin transaction
22:07:18 web.1 | User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'user@example.com' AND "users"."id" != 2) LIMIT 1
22:07:18 web.1 | User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE ("users"."display_name" = 'User#1' AND "users"."id" != 2) LIMIT 1
22:07:18 web.1 | (0.1ms) rollback transaction
22:07:18 web.1 | CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "2"]]
22:07:18 web.1 | Completed 204 No Content in 60ms (ActiveRecord: 23.5ms)
正如你所看到的,有一个回滚事务,其中我无法找到它发生的原因(检查下面的答案)或如何从中获取更多细节(我能够从中获得的唯一信息是使用Rails Logger,但信息只是最后附加了错误{}的模型)。然而,创建用户工作正常,我也有类似的实现另一个模型(帖子)也工作,所以通过自然演绎,我倾向于说,Devise是罪魁祸首。我确实尝试从this blog post实施非密码用户更新,但我仍然遇到同样的问题。
该项目是开源的,可以在github上的Deovandski / Fakktion下找到,但这里是我认为最重要的代码片段:
Rails路由器
...
devise_for :users, controllers: {sessions: 'sessions'}
namespace :api do
namespace :v1 do
...
resources :users
...
Rails User_controller
...
def show
respond_with user
end
def create
respond_with :api, :v1, User.create(user_params)
end
def update
if user_params[:password].blank?
user_params.delete(:password)
user_params.delete(:password_confirmation)
end
successfully_updated = if needs_password?(user, user_params)
user.update(user_params)
else
user.update_without_password(user_params)
end
if successfully_updated
respond_with user, status: :ok
else
respond_with user.errors, status: :unprocessable_entity
end
end
...
private
def user
User.find(params[:id])
end
def needs_password?(user, params)
params[:password].present?
end
def user_params
params.require(:user).permit(:id, :show_full_name, :full_name, :display_name, :email, :date_of_birth, :gender, :facebook_url, :twitter_url, :personal_message, :webpage_url, :is_banned, :is_banned_date, :legal_terms_read, :privacy_terms_read, :is_admin, :is_super_user, :sign_in_count, :password, :number_of_comments, :number_of_posts, :last_sign_in_at, :reset_password_sent_at, :reset_password_token, :updated_at, :created_at)
end
答案 0 :(得分:0)
由于缺少错误或任何有用的信息,这个错误搜索需要一段时间,结果是在rails端的用户模型结束。更具体地说,缺少这两条线:
validates :password, length: {minimum: 8}, presence: true, on: :create
validates :password, length: {minimum: 8}, on: :update, allow_blank: true
在旁注中,现在客户端可以在设置或不设置新密码的情况下更新他们的信息(只要他们经过身份验证。)我的目标是在用户尝试进行更改时要求当前密码,所以请告诉我如果你想看到这样的实现。