用JsonB字段设计注册“字符串不匹配”

时间:2019-02-05 00:37:14

标签: ruby-on-rails devise ruby-on-rails-5 jsonb

当我删除:location字段时,该窗体有效。但是,我需要获取它来存储jsonb数据。

RegistrationsController#create中的IndexError

  

字符串不匹配

 Extracted source (around line #151):

          if value != read(object, attribute, key)
            object.public_send :"#{attribute}_will_change!"
            object.public_send(attribute)[key] = value
          end
        end

RegistrationsController

class RegistrationsController < Devise::RegistrationsController
  protected


  private 

  def sign_up_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :location)
  end

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password, :location)
  end

end

架构

t.jsonb "interests", default: "{}", null: false

模型

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  # These fields are within the interests jsonb field
  store_accessor :interests, :location

end

表格

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= bootstrap_devise_error_messages! %>

  <div class="form-group">
    <%= f.label :email %>
    <%= f.email_field :email, autofocus: true, autocomplete: 'email', class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.label :location %>
    <%= f.text_field :location, class: 'form-control' %>
  </div>

1 个答案:

答案 0 :(得分:0)

错误在于

  

默认:“ {}”

正在迁移。它必须是

  

{}

前者是一个字符串,后者是一个哈希。您不能将密钥分配给哈希。