如何从 AMS 0.10.0.rc4 反序列化中删除参数?
How to remove parameters from AMS 0.10.0.rc4 Deserialization?
给出以下 user_params:
def user_params
ActiveModelSerializers::Deserialization.jsonapi_parse!(params.to_unsafe_h)
end
我无法删除参数以执行需要删除 :current_password
的特定操作。以前下面的行完成了这项工作:
user_params.delete(:current_password)
但是,自从实施 0.10.0.rc4 以来,相关操作生成 ActiveRecord::UnknownAttributeError(用户的未知属性 'current_password'。)
user.update_without_password(user_params)
我不确定这是错误还是语法错误,所以我决定在 SO 上 post 而不是他们的 Github 仓库。
更新 #1
应BF4的要求,我在AMS Github上创建了issue #1610关于这个问题。同时,下面的原始 post 是创建最终修复程序时的解决方法。
原版Post
我找不到删除参数的方法,但使用 .except(a)
可以像下面的 Update 方法那样解决问题:
# Render the updated User using UserSerializer and the AMS Deserialization.
def update
# Check if password is blank, if so, clear :current_password
# and update without password, else updates password.
if user_params[:password].blank? || user_params[:current_password].blank?
user.update_without_password(user_params.except(:current_password,:password))
render json: user
else
if user.update_with_password(user_params)
render json: user, status: :ok
else
render json: user.errors, status: :unprocessable_entity
end
end
end
给出以下 user_params:
def user_params
ActiveModelSerializers::Deserialization.jsonapi_parse!(params.to_unsafe_h)
end
我无法删除参数以执行需要删除 :current_password
的特定操作。以前下面的行完成了这项工作:
user_params.delete(:current_password)
但是,自从实施 0.10.0.rc4 以来,相关操作生成 ActiveRecord::UnknownAttributeError(用户的未知属性 'current_password'。)
user.update_without_password(user_params)
我不确定这是错误还是语法错误,所以我决定在 SO 上 post 而不是他们的 Github 仓库。
更新 #1
应BF4的要求,我在AMS Github上创建了issue #1610关于这个问题。同时,下面的原始 post 是创建最终修复程序时的解决方法。
原版Post
我找不到删除参数的方法,但使用 .except(a)
可以像下面的 Update 方法那样解决问题:
# Render the updated User using UserSerializer and the AMS Deserialization.
def update
# Check if password is blank, if so, clear :current_password
# and update without password, else updates password.
if user_params[:password].blank? || user_params[:current_password].blank?
user.update_without_password(user_params.except(:current_password,:password))
render json: user
else
if user.update_with_password(user_params)
render json: user, status: :ok
else
render json: user.errors, status: :unprocessable_entity
end
end
end