1

我正在尝试使用 rails 和 devise_token_auth 进行身份验证。我成功地为 devise_token_auth 创建了一个新用户。我已经在谷歌或在这个论坛中搜索了这个,但我找不到关于如何去做的可靠文档。但是如果你给我链接。对于新的 Rails 开发人员来研究它会更加感激。我正在尝试添加用户的个人资料。在用户表中,我添加了配置文件的引用,这是结果。

在此处输入图像描述

如您所见profile_id,我的用户表中已添加。在我的用户模型中,我有这个。

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :omniauthable
  include DeviseTokenAuth::Concerns::User 

  belongs_to :profile
end

也在我的个人资料模型中。我有这个

class Profile < ApplicationRecord
    has_one :user
end

在我的 application_controller.rb 中。我有这个

class ApplicationController < ActionController::Base
    include DeviseTokenAuth::Concerns::SetUserByToken
    before_action :configure_permitted_parameters, if: :devise_controller?
    #protect_from_forgery with: :exception 

    protected

    def configure_permitted_parameters
        registration_params = [ :name, :city, :email, :password, :password_confirmation, :profile => [:address]]
        devise_parameter_sanitizer.permit(:sign_up, keys: registration_params)
    end
end

我尝试使用邮递员来注册用户来设计。我有这些参数要发送

{
    "name": "rayn",
    "email": "helloworld.232910@gmail.com",
    "password": "sample123",
    "password_confirmation": "sample123",
    "city": "green",
    "profile": {
        "address": "address here"
    },
    "confirm_success_url": "http://localhost:3000"
}

但是当我提交这个时,我得到了这些错误

{
    "status": 500,
    "error": "Internal Server Error",
    "exception": "#<ActiveRecord::AssociationTypeMismatch: Profile(#70239077093620) expected, got {\"address\"=>\"address here\"} which is an instance of ActiveSupport::HashWithIndifferentAccess(#47165751411560)>",
    "traces": {

我不知道我的模型是否正确。我只想为每个用户添加个人资料。纠正我在代码中所做的事情。如何注册已添加个人资料的用户?

4

1 回答 1

0

我认为您错过了accepts_nested_attribute,因此active_record无法了解如何处理该对象 - 假设,有一个列地址Profile

# user.rb
accepts_nested_attributes_for :profile
于 2019-04-19T09:54:09.780 回答