无法使用Devise注册具有嵌套属性的新用户

时间:2017-09-14 18:31:26

标签: ruby-on-rails activerecord devise pg

使用Devise尝试使用嵌套属性注册新用户时遇到了很大麻烦。我已经搜索了很多,但我没有设法解决我的问题。

我有一个标准的用户模型,有一些额外的属性。 此用户has_one地址,这是另一个具有地址属性的模型。它还有_one telephone_number,这是另一个具有电话号码属性的型号。

我已经设置了所有内容,但是当我点击“注册”按钮时,我收到500内部服务器错误并显示以下消息:

PG::UndefinedColumn: ERROR: column users.user_id does not exist LINE 1: SELECT 1 AS one FROM "users" WHERE "users"."user_id" IS NUL... ^ : SELECT 1 AS one FROM "users" WHERE "users"."user_id" IS NULL LIMIT $1

users.user_id怎么可能不存在?

这是堆栈轨道(非常大)

Started POST "/users" for 127.0.0.1 at 2017-09-14 17:08:50 -0300
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"rUHhEAqAk5Jp/hvyTqF09ButGdsVpycGl5s7BDt/yKb8UaW4loWMmvpAj2NcMEPFEY68xDA45feMRsotIOaagg==", "user"=>{"first_name"=>"teste", "last_name"=>"teste", "email"=>"teste@teste.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "professional"=>"1", "artistic_name"=>"teste", "address_attributes"=>{"street"=>"teste", "number"=>"212"}, "telephone_number_attributes"=>{"ddd"=>"21", "number"=>"98989898"}}, "commit"=>"Sign up"}
   (0.1ms)  BEGIN
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "teste@teste.com"], ["LIMIT", 1]]
  User Exists (0.2ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "teste@teste.com"], ["LIMIT", 1]]
  User Exists (0.4ms)  SELECT  1 AS one FROM "users" WHERE "users"."user_id" IS NULL LIMIT $1  [["LIMIT", 1]]
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 159ms (ActiveRecord: 1.2ms)



ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR:  column users.user_id does not exist
LINE 1: SELECT  1 AS one FROM "users" WHERE "users"."user_id" IS NUL...
                                            ^
: SELECT  1 AS one FROM "users" WHERE "users"."user_id" IS NULL LIMIT $1):

activerecord (5.0.5) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
activerecord (5.0.5) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
activerecord (5.0.5) lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
activesupport (5.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (5.0.5) lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
activerecord (5.0.5) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
activerecord (5.0.5) lib/active_record/connection_adapters/postgresql_adapter.rb:587:in `execute_and_clear'
activerecord (5.0.5) lib/active_record/connection_adapters/postgresql/database_statements.rb:13:in `select_value'
activerecord (5.0.5) lib/active_record/relation/finder_methods.rb:335:in `exists?'
activerecord (5.0.5) lib/active_record/validations/uniqueness.rb:29:in `validate_each'
activemodel (5.0.5) lib/active_model/validator.rb:151:in `block in validate'
activemodel (5.0.5) lib/active_model/validator.rb:148:in `each'
activemodel (5.0.5) lib/active_model/validator.rb:148:in `validate'
activesupport (5.0.5) lib/active_support/callbacks.rb:405:in `public_send'
activesupport (5.0.5) lib/active_support/callbacks.rb:405:in `block in make_lambda'
activesupport (5.0.5) lib/active_support/callbacks.rb:169:in `block (2 levels) in halting'
activesupport (5.0.5) lib/active_support/callbacks.rb:547:in `block (2 levels) in default_terminator'
activesupport (5.0.5) lib/active_support/callbacks.rb:546:in `catch'
activesupport (5.0.5) lib/active_support/callbacks.rb:546:in `block in default_terminator'
activesupport (5.0.5) lib/active_support/callbacks.rb:170:in `block in halting'
activesupport (5.0.5) lib/active_support/callbacks.rb:454:in `block in call'
activesupport (5.0.5) lib/active_support/callbacks.rb:454:in `each'
activesupport (5.0.5) lib/active_support/callbacks.rb:454:in `call'
activesupport (5.0.5) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.5) lib/active_support/callbacks.rb:750:in `_run_validate_callbacks'
activemodel (5.0.5) lib/active_model/validations.rb:408:in `run_validations!'
activemodel (5.0.5) lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
activesupport (5.0.5) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.5) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.5) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.5) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.5) lib/active_support/callbacks.rb:750:in `_run_validation_callbacks'
activemodel (5.0.5) lib/active_model/validations/callbacks.rb:113:in `run_validations!'
activemodel (5.0.5) lib/active_model/validations.rb:338:in `valid?'
activerecord (5.0.5) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.0.5) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.0.5) lib/active_record/validations.rb:44:in `save'
activerecord (5.0.5) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (5.0.5) lib/active_record/transactions.rb:319:in `block (2 levels) in save'
activerecord (5.0.5) lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
activerecord (5.0.5) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
activerecord (5.0.5) lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
activerecord (5.0.5) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
activerecord (5.0.5) lib/active_record/transactions.rb:211:in `transaction'
activerecord (5.0.5) lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
activerecord (5.0.5) lib/active_record/transactions.rb:319:in `block in save'
activerecord (5.0.5) lib/active_record/transactions.rb:334:in `rollback_active_record_state!'
activerecord (5.0.5) lib/active_record/transactions.rb:318:in `save'
activerecord (5.0.5) lib/active_record/suppressor.rb:41:in `save'
devise (4.3.0) app/controllers/devise/registrations_controller.rb:17:in `create'
actionpack (5.0.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.5) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.5) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.5) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.5) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.5) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.5) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.5) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.5) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.5) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.5) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.5) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.5) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.5) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.5) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.5) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.5) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.0.5) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.5) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.5) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.5) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.5) lib/action_dispatch/routing/route_set.rb:727:in `call'
rack-pjax (1.0.0) lib/rack/pjax.rb:12:in `call'
/usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/bundler/gems/remotipart-cd653ddc43bb/lib/remotipart/middleware.rb:32:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.5) lib/active_record/migration.rb:553:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.5) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.5) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.5) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.5) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.0.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.5) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.0.5) lib/rails/engine.rb:522:in `call'
puma (3.10.0) lib/puma/configuration.rb:225:in `call'
puma (3.10.0) lib/puma/server.rb:605:in `handle_request'
puma (3.10.0) lib/puma/server.rb:437:in `process_client'
puma (3.10.0) lib/puma/server.rb:301:in `block in run'
puma (3.10.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (4.1ms)
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
  Rendering /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.2ms)
  Rendered /usr/local/opt/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/actionpack-5.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (22.1ms)

所以,如果有人可以提供帮助,这是我的代码:

模式

# users table
create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "",    null: false
    t.string   "encrypted_password",     default: "",    null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.string   "first_name",                             null: false
    t.string   "last_name",                              null: false
    t.string   "artistic_name"
    t.boolean  "admin",                  default: false, null: false
    t.boolean  "professional",           default: false, null: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  end

# addresses table
create_table "addresses", force: :cascade do |t|
    t.integer  "user_id"
    t.string   "street"
    t.integer  "number"
    t.string   "zip_code"
    t.string   "complement"
    t.string   "district"
    t.string   "city"
    t.string   "state"
    t.boolean  "public",     default: false
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
    t.index ["user_id"], name: "index_addresses_on_user_id", using: :btree
  end

# telephone_numbers table
create_table "telephone_numbers", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "ddd"
    t.string   "number"
    t.boolean  "public",     default: true
    t.datetime "created_at",                null: false
    t.datetime "updated_at",                null: false
    t.index ["user_id"], name: "index_telephone_numbers_on_user_id", using: :btree
  end

我的模特

# user.rb
class User < ApplicationRecord
    ...
    has_one :address, inverse_of: :user
    has_one :telephone_number, inverse_of: :user
    accepts_nested_attributes_for :address, :telephone_number
    ...
end

# address.rb
class Address < ApplicationRecord
    belongs_to :user
end

# telephone_number.rb
class TelephoneNumber < ApplicationRecord
    belongs_to :user
end

我的控制器

# application_controller.rb
class ApplicationController < ActionController::Base
    ...
    before_action :configure_permitted_parameters, if: :devise_controller?
    def configure_permitted_parameters
        # Aditional parameters for user
        devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :artistic_name, :professional,
        address_attributes: [:street, :number, :user_id, :complement, :zip_code, :city, :district, :state, :public],
        telephone_number_attributes: [:ddd, :number]])
    end
end

# /users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
    def new
      resource = build_resource({})
      resource.build_address
      resource.build_telephone_number
      respond_with resource
    end
end

我的注册表格

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= f.error_notification %>

    <div class="form-inputs">
        <%= f.input :first_name, required: true, autofocus: true %>
        <%= f.input :last_name, required: true %>
        <%= f.input :email, required: true %>
        <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
        <%= f.input :password_confirmation, required: true %>

        <%= f.input :professional %>

        <%= f.input :artistic_name %>

        <%= f.simple_fields_for :address do |a| %>
            <%= a.input :street %>
            <%= a.input :number %>
        <% end %>

        <%= f.simple_fields_for :telephone_number do |t| %>
            <%= t.input :ddd %>
            <%= t.input :number %>
        <% end %>

        <div class="form-actions">
            <%= f.button :submit, "Sign up" %>
        </div>
    </div>
<% end %>

<%= render "devise/shared/links" %>

最后,但并非最不重要

/config/routes.rb

Rails.application.routes.draw do
    ...
    devise_for :users, controllers: { registrations: 'users/registrations' }
    ...
end

真的很感谢你的帮助, 谢谢!

1 个答案:

答案 0 :(得分:0)

如果您看到堆栈跟踪,则问题与验证有关,特别是使用唯一性验证。它首先说已经存在具有该邮件的用户。因此,请尝试使用其他电子邮件创建用户。

您是否在用户模型中进行了另一次验证,错误地说明了user_id而不只是id

activerecord (5.0.5) lib/active_record/validations/uniqueness.rb:29:in `validate_each'