Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:用户信息未更新(呈现的ActiveModel序列化程序为Null,带有哈希)_Ruby On Rails_Ruby_Api_Serialization_Activerecord - Fatal编程技术网

Ruby on rails Rails:用户信息未更新(呈现的ActiveModel序列化程序为Null,带有哈希)

Ruby on rails Rails:用户信息未更新(呈现的ActiveModel序列化程序为Null,带有哈希),ruby-on-rails,ruby,api,serialization,activerecord,Ruby On Rails,Ruby,Api,Serialization,Activerecord,同样的代码在本地工作,但在Heroku中不工作 def update current_user.update!(user_params) response = { message: Message.account_updated} json_response(response) end private def user_params params.permit(:first_name, :last_name, :email, :password) end rou

同样的代码在本地工作,但在Heroku中不工作

def update
    current_user.update!(user_params)
    response = { message: Message.account_updated}
    json_response(response)
end
private
def user_params
    params.permit(:first_name, :last_name, :email, :password)
end
routes.rb

namespace :api do
    namespace :v1 do
        put 'user/update', to: 'users#update'
    end
end
user_serializer.rb

class UserSerializer < ActiveModel::Serializer
    attributes :id, :username, :first_name, :last_name, :email, :created_at, :updated_at
end
我想这就是问题所在

呈现的ActiveModel::Serializer::Null和哈希(0.51ms)

但我不明白发生了什么

更新

module Response
    def json_response(object, status = :ok)
        render json: object, status: status
    end
end
首先

呈现的ActiveModel::Serializer::Null和哈希(0.51ms)

这不是不更新用户信息的问题,它显示在日志中是因为
ActiveModel::Serializer
序列化具有
null
值的列

我认为问题出在其他地方,比如你需要重构,为什么会这样,比如你可以尝试删除
更新!(用户参数)
像这样尝试
当前用户。更新(用户参数)
或任何其他

# place this method inside NullAttributesRemover or directly inside serializer class
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
  hash = super
  hash.each { |key, value| hash.delete(key) if value.nil? }
  hash
end
它将序列化只有实值的列


我想这会有帮助。

什么是
json\u响应
方法?它是从某个图书馆来的还是你自己写的?你能粘贴它的代码吗?@AntonTkachov看到更新部分,输入
json\u响应
尝试用Serializer指定一个特定的序列化程序:UserSerializer?它似乎没有猜测序列化程序类,因为您已将代码提取到模块中。但它也会抛出500个错误,因此完整的stacktrace会很有用。
# place this method inside NullAttributesRemover or directly inside serializer class
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
  hash = super
  hash.each { |key, value| hash.delete(key) if value.nil? }
  hash
end