如何在gem" devise"中更新相关模型account_update操作

时间:2015-06-26 12:54:55

标签: ruby-on-rails ruby ruby-on-rails-4 devise

我是铁轨上的红宝石

我使用gem"设计"对于身份验证用户,我在帐户更新操作中遇到更新已关联模型的问题。在帐户更新时,密钥' user_id'在相关记录中设置为nil

发生的数据库查询:

UPDATE "user_information" SET "user_id" = ? WHERE "user_information"."user_id" = ?  [["user_id", nil], ["user_id", 1]]
UPDATE "user_settings" SET "user_id" = ? WHERE "user_settings"."user_id" = ?  [["user_id", nil], ["user_id", 1]]

即请求关联模型中的密钥user_id为nil。我做错了什么?

我的代码:

迁移

create_table(:users) do |t|
  t.string :email,              null: false, default: ""
  t.string :encrypted_password, null: false, default: ""
  # etc
end

create_table :user_settings, :id => false do |t|
  t.references :user, null: false, index: true, unique: true
  # etc
end

create_table :user_information, :id => :user_id do |t|
  t.references :user, null: false, index: true, unique: true
  # etc
end

用户模型

class User < ActiveRecord::Base
  has_one :setting, :class_name => 'User::Setting', inverse_of: :user
  has_one :information, :class_name => 'User::Information', inverse_of: :user

  accepts_nested_attributes_for :setting
  accepts_nested_attributes_for :information'

  # devise
end

注册控制器

class Users::Devise::RegistrationsController < Devise::RegistrationsController
  before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:account_update) do |u|
      u.permit(:name, :email, :password, :password_confirmation, :current_password,
           information_attributes: [:name, :contacts], setting_attributes: [:monetization])
        # etc
    end
  end

  def build_resource(hash=nil)
    super
    self.resource.build_setting
    self.resource.build_information if hash.empty?
  end
end

0 个答案:

没有答案