Rails,Devise中的无效外键:: RegistrationsController#destroy

时间:2016-06-10 20:30:01

标签: ruby-on-rails devise registration

错误:

PG :: ForeignKeyViolation:错误:更新或删除表"用户"违反外键约束" fk_rails_03de2dc08c"在桌子上"评论"详细信息:Key(id)=(6)仍然从表"注释"中引用。 :DELETE FROM"用户"用户"。" id" = $ 1

def exec_no_cache(sql, name, binds)
  type_casted_binds = binds.map { |attr| type_cast(attr.value_for_database) }
  log(sql, name, binds) { @connection.async_exec(sql, type_casted_binds) } [HIGHLIGHTING THIS LINE]
end

def exec_cache(sql, name, binds)

发生了什么:

所以我是ruby和rails的新手,只是让应用程序学习 - 我正在为用户使用Devise,每当我尝试在我的应用程序上编辑用户配置文件时,它将

A。)工作正常

B.)打破这个错误。

(通常在交替循环中它会断开,如果我重新加载页面然后再继续它将起作用。然后如果我再次尝试编辑用户它将会中断,并且循环继续)

终端:

(采取的措施:加载主页 - >加载编辑用户页面 - >提交编辑(我没有更改任何值) - >错误 - >点击返回,加载编辑页面 - >提交编辑(我没有更改任何值) - >工作。)

Kegans-MacBook-Pro:MyApp Kegan$ rails s
Array values in the parameter to `Gem.paths=` are deprecated.
Please use a String or nil.
An Array ({"GEM_PATH"=>["/Users/Kegan/.rvm/gems/ruby-2.3.1", "/Users/Kegan/.rvm/gems/ruby-2.3.1@global"]}) was passed in from bin/rails:3:in `load'
=> Booting Puma
=> Rails 5.0.0.rc1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
[Simple Form] Simple Form is not configured in the application and will use the default values. Use `rails generate simple_form:install` to generate the Simple Form configuration.
Puma starting in single mode...
* Version 3.4.0 (ruby 2.3.1-p112), codename: Owl Bowl Brawl
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Started GET "/" for ::1 at 2016-06-10 14:15:05 -0600
  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by HomeController#index as HTML
  Rendering home/index.html.erb within layouts/application
  User Load (0.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Link Load (0.5ms)  SELECT  "links".* FROM "links" ORDER BY "links"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "links".* FROM "links" ORDER BY "links"."id" DESC LIMIT $1  [["LIMIT", 1]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (1.0ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 8], ["votable_type", "Link"], ["vote_flag", true]]
   (0.4ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 8], ["votable_type", "Link"], ["vote_flag", false]]
  Forum Load (0.5ms)  SELECT  "forums".* FROM "forums" ORDER BY "forums"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "forums".* FROM "forums" ORDER BY "forums"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Rendered home/index.html.erb within layouts/application (84.0ms)
Completed 200 OK in 291ms (Views: 263.2ms | ActiveRecord: 16.4ms)


Started GET "/cable" for ::1 at 2016-06-10 14:15:05 -0600
Started GET "/cable/" [WebSocket] for ::1 at 2016-06-10 14:15:05 -0600
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
[ActionCable] [joshgamer1@gmail.com] Registered connection (Z2lkOi8va29ob3JkL1VzZXIvNg)
[ActionCable] [joshgamer1@gmail.com] RoomChannel is transmitting the subscription confirmation
[ActionCable] [joshgamer1@gmail.com] RoomChannel is streaming from room_channel
Started GET "/users/edit" for ::1 at 2016-06-10 14:15:10 -0600
Processing by Devise::RegistrationsController#edit as HTML
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(sign_up) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:sign_up) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/AppDev/myApp/app/controllers/application_controller.rb:10)
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(account_update) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:account_update) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/AppDev/MyApp/app/controllers/application_controller.rb:11)
  Rendering devise/registrations/edit.html.erb within layouts/application
  Rendered devise/registrations/edit.html.erb within layouts/application (14.5ms)
Completed 200 OK in 81ms (Views: 77.5ms | ActiveRecord: 0.5ms)


Started DELETE "/users" for ::1 at 2016-06-10 14:15:18 -0600
Processing by Devise::RegistrationsController#destroy as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9Ou51KmxWKW+SheDAZl86Rtb8jMQBDSVZswiwfkHxUpg2b4DA/9xneXzPXsckQ7V6oy6MqBkWPctS24X1Z9V7w==", "user"=>{"avatar"=>"", "name"=>"Joshua Artos", "email"=>"joshgamer1@gmail.com", "password"=>"[FILTERED]", "bio"=>"Josh. Kick ass at video games.", "location"=>"Fort Collins", "age"=>"18", "gender"=>"Doesn't Matter", "interestedin"=>"Netfix", "major"=>"Doesn't Matter", "twitterurl"=>"", "instagramurl"=>"", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(sign_up) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:sign_up) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/AppDev/MyApp/app/controllers/application_controller.rb:10)
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(account_update) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:account_update) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/MyApp/Kohord/app/controllers/application_controller.rb:11)
   (0.1ms)  BEGIN
  SQL (2.3ms)  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 6]]
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 10ms (ActiveRecord: 3.0ms)



ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR:  update or delete on table "users" violates foreign key constraint "fk_rails_03de2dc08c" on table "comments"
DETAIL:  Key (id)=(6) is still referenced from table "comments".
: DELETE FROM "users" WHERE "users"."id" = $1):

activerecord (5.0.0.rc1) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `block in exec_no_cache'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
activesupport (5.0.0.rc1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `exec_no_cache'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/postgresql_adapter.rb:587:in `execute_and_clear'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/postgresql/database_statements.rb:116:in `exec_delete'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract/database_statements.rb:140:in `delete'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
activerecord (5.0.0.rc1) lib/active_record/relation.rb:543:in `delete_all'
activerecord (5.0.0.rc1) lib/active_record/persistence.rb:525:in `destroy_row'
activerecord (5.0.0.rc1) lib/active_record/counter_cache.rb:141:in `destroy_row'
activerecord (5.0.0.rc1) lib/active_record/locking/optimistic.rb:115:in `destroy_row'
activerecord (5.0.0.rc1) lib/active_record/persistence.rb:184:in `destroy'
activerecord (5.0.0.rc1) lib/active_record/callbacks.rb:283:in `block in destroy'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_destroy_callbacks'
activerecord (5.0.0.rc1) lib/active_record/callbacks.rb:283:in `destroy'
activerecord (5.0.0.rc1) lib/active_record/transactions.rb:314:in `block in destroy'
activerecord (5.0.0.rc1) lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
activerecord (5.0.0.rc1) lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
activerecord (5.0.0.rc1) lib/active_record/transactions.rb:211:in `transaction'
activerecord (5.0.0.rc1) lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
activerecord (5.0.0.rc1) lib/active_record/transactions.rb:314:in `destroy'
devise (4.1.1) app/controllers/devise/registrations_controller.rb:66:in `destroy'
actionpack (5.0.0.rc1) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:181:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/rescue.rb:31:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0.rc1) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0.rc1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0.rc1) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0.rc1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0.rc1) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0.rc1) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0.rc1) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:16:in `block in <class:Constraints>'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/mapper.rb:46:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0.rc1) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0.rc1) lib/action_dispatch/routing/route_set.rb:725:in `call'
actionview (5.0.0.rc1) lib/action_view/digestor.rb:12:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.0.rc1) lib/rack/etag.rb:25:in `call'
rack (2.0.0.rc1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.0.rc1) lib/rack/head.rb:12:in `call'
rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.0.rc1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0.rc1) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0.rc1) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0.rc1) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0.rc1) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.0.rc1) lib/rack/method_override.rb:22:in `call'
rack (2.0.0.rc1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0.rc1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0.rc1) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.0.rc1) lib/rack/sendfile.rb:111:in `call'
railties (5.0.0.rc1) lib/rails/engine.rb:522:in `call'
puma (3.4.0) lib/puma/configuration.rb:224:in `call'
puma (3.4.0) lib/puma/server.rb:569:in `handle_request'
puma (3.4.0) lib/puma/server.rb:406:in `process_client'
puma (3.4.0) lib/puma/server.rb:271:in `block in run'
puma (3.4.0) lib/puma/thread_pool.rb:114:in `block in spawn_thread'
  Rendering /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (6.5ms)
  Rendering /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
  Rendering /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
  Rendered /Users/KingKegan/.rvm/gems/ruby-2.3.1/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (52.5ms)

(删除了一些缓解字符限制的渲染)

[ActionCable] [joshgamer1@gmail.com] Finished "/cable/" [WebSocket] for ::1 at 2016-06-10 14:15:18 -0600
[ActionCable] [joshgamer1@gmail.com] RoomChannel stopped streaming from room_channel
Started GET "/cable" for ::1 at 2016-06-10 14:15:21 -0600
Started GET "/cable/" [WebSocket] for ::1 at 2016-06-10 14:15:21 -0600
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
[ActionCable] [joshgamer1@gmail.com] Registered connection (Z2lkOi8va29ob3JkL1VzZXIvNg)
[ActionCable] [joshgamer1@gmail.com] RoomChannel is transmitting the subscription confirmation
[ActionCable] [joshgamer1@gmail.com] RoomChannel is streaming from room_channel
Started PUT "/users" for ::1 at 2016-06-10 14:15:28 -0600
Processing by Devise::RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MfnKmUW4Xfct9NyjC9bbwfEDusLvTYXHQRuqyltHr1yly81O7/Z0z3ZN9lsW3qn9ANTyw18t6aUKnOYcd98/+Q==", "user"=>{"avatar"=>"", "name"=>"Joshua Artos", "email"=>"joshgamer1@gmail.com", "password"=>"[FILTERED]", "bio"=>"Josh. Kick ass at video games.", "location"=>"Fort Collins", "age"=>"18", "gender"=>"Doesn't Matter", "interestedin"=>"Netfix", "major"=>"Doesn't Matter", "twitterurl"=>"", "instagramurl"=>"", "current_password"=>"[FILTERED]"}, "commit"=>"Update"}
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(sign_up) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:sign_up) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/AppDev/MyApp/app/controllers/application_controller.rb:10)
DEPRECATION WARNING: [Devise] Changing the sanitized parameters through "Devise::ParameterSanitizer#for(account_update) is deprecated and it will be removed from Devise 4.2.
Please use the `permit` method:

  devise_parameter_sanitizer.permit(:account_update) do |user|
    # Your block here.
  end
 (called from configure_permitted_parameters at /Users/Kegan/Documents/AppDev/MyApp/app/controllers/application_controller.rb:11)
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 94ms (ActiveRecord: 1.3ms)


Started GET "/" for ::1 at 2016-06-10 14:15:28 -0600
Processing by HomeController#index as HTML
  Rendering home/index.html.erb within layouts/application
  User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Link Load (0.3ms)  SELECT  "links".* FROM "links" ORDER BY "links"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "links".* FROM "links" ORDER BY "links"."id" DESC LIMIT $1  [["LIMIT", 1]]
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (0.5ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 8], ["votable_type", "Link"], ["vote_flag", true]]
   (0.9ms)  SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = $1 AND "votes"."votable_type" = $2 AND "votes"."vote_flag" = $3 AND "votes"."vote_scope" IS NULL  [["votable_id", 8], ["votable_type", "Link"], ["vote_flag", false]]
  Forum Load (0.3ms)  SELECT  "forums".* FROM "forums" ORDER BY "forums"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "forums".* FROM "forums" ORDER BY "forums"."id" DESC LIMIT $1  [["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Rendered home/index.html.erb within layouts/application (14.5ms)
Completed 200 OK in 56ms (Views: 51.7ms | ActiveRecord: 2.8ms)


[ActionCable] [joshgamer1@gmail.com] Finished "/cable/" [WebSocket] for ::1 at 2016-06-10 14:15:28 -0600
[ActionCable] [joshgamer1@gmail.com] RoomChannel stopped streaming from room_channel
Started GET "/cable" for ::1 at 2016-06-10 14:15:28 -0600
Started GET "/cable/" [WebSocket] for ::1 at 2016-06-10 14:15:28 -0600

部分代码:

用户模型:

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

  has_many :forums
  has_many :links
  has_many :discussions
  has_many :comments

  has_many :chats
  has_many :chatrooms, through: :chats




# It returns the articles whose titles contain one or more words that form the query
  def self.search(query)
    # where(:title, query) -> This would return an exact match of the query
    where("name like ?", "%#{query}%") 
  end


  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missingMike.png"
  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
end

用户控制器:

class UsersController < ApplicationController


  def index

    if params[:search]
      @users = User.search(params[:search])
      @users = @users.paginate(:page => params[:page], :per_page => 5)
    else
      @users = User.paginate(:page => params[:page], :per_page => 2)
    end
  end


  def show
    @user = User.find(params[:id])
    @user_links = @user.links
    @user_comments = @user.comments 
    @user_forums = @user.forums 
    @user_discussions = @user.discussions 

  end

  def goAnonymous
    @user = User.find(params[:id])
    @user.anonymous = true
    redirect_to :back
  end

  def leaveAnonymous
    @user = User.find(params[:id])
    @user.anonymous = false
    redirect_to :back
  end

end

应用程序控制器

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :current_password, :avatar, :bio, :gender, :location, :interestedin, :major, :age, :instagramurl, :twitterurl, :anonymous) }
  end

end

/views/devise/registrations/edit.html.erb

<h2 class="foldersBox">Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>


  <div class="foldersBox card-wrap">
    <div>

      <div>

        <div id="label">
          <%= f.label :avatar %>
          <%= f.file_field :avatar, class: "form-control" %>
        </div>

        <div>
          <%= f.label :name %>
          <%= f.text_field :name, class: "form-control", :autofocus => true %>
        </div>

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

        <div>
          <%= f.label :password %> <i>(leave blank if you don't want to change it)</i>
          <%= f.password_field :password, class: "form-control", :autocomplete => "off" %>
        </div>


        <br>

        <h4>Fun Stuff:</h4>

        <div>
          Bio: (At least try to be funny)<br>
          <%= f.text_area :bio, class: "form-control" %>
        </div>

        <div>
          Location:<br>
          <%= f.text_field :location, class: "form-control" %>
        </div>

        <div>
          Age: (Most likely the brief time frame when you won't lie about your age (21-24))br>
          <%= f.select :age, (18..99), class: "form-control" %>
        </div>

        <div>
          Gender (Whatever is PC right now):<br>
          <%= f.text_field :gender, class: "form-control" %>
        </div>

        <div>
          Interested In:<br>
          <%= f.text_area :interestedin, class: "form-control" %>
        </div>

        <div>
          Major: (Like you won't change in a semester, probably just leave blank)<br>
          <%= f.text_area :major, class: "form-control" %>
        </div>


        <h4>Social Media Stuff</h4>

        <div>
          Twitter:<br>
          @<%= f.text_field :twitterurl, class: "form-control" %>
        </div>

        <div>
          Instagram:<br>
          @<%= f.text_field :instagramurl, class: "form-control" %>
        </div>






        <div>
          <strong><%= f.label :current_password, class: "currentPasswordForm" %></strong> <i>(we need your current password to confirm your changes)</i>
          <%= f.password_field :current_password, autocomplete: "off", class: "form-control" %>
        </div>


      </div>
      <div>
        <%= f.submit "Update", class: "updateBTN" %>
      </div>

    <% end %>
  </div>
  <div>
    <br><br>

    <br><i>
    Cancel my account<br>
    Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: {
      confirm: "Are you sure?" }, method: :delete %>
    </i>

  </div>
</div>

迁移评论:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.integer :link_id
      t.text :body
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
    end
    add_index :comments, :link_id
  end
end

评论模型:

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :link
end

3 个答案:

答案 0 :(得分:1)

如果您将数据库回滚到迁移之前(db:rollback或db:migrate:down VERSION =迁移文件名的数字部分。)

最后将您的迁移更改为引用链接,而不仅仅是link_id。重新运行您的迁移:

来自

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.integer :link_id
      t.text :body
      t.references :user, index: true, foreign_key: true

      t.timestamps null: false
  end
    add_index :comments, :link_id
 end
end

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.text :body
      t.references :user, index: true, foreign_key: true
      t.references :link, index: true, foreign_key: true

      t.timestamps null: false
  end
    add_index :comments
 end
end

要通过迁移执行此操作,您需要执行以下操作:

rails g migration RemoveLink_IdFromComments

def change
  remove_column :comments, :link_id
end

rails g migration AddLinkToComments

def change
  add_reference :comments, :link, index: true, foreign_key: true
end

答案 1 :(得分:0)

我相信Activity导致了这个问题。我想你需要摆脱它并再次添加对你的表的链接引用并尝试。

belongs_to :link

运行rake db:migrate并重试。如果能解决您的问题,请告诉我。

rails g migration RemoveLinkIdFromComments link_id:integer
rails g migration AddLinkRefToComments link:references
#Do check your newly created migration files it everything is correct.

如果我们查看错误,我们可以看到我们的关联错误。 您可以删除开发数据库中的所有内容(所有数据),然后重试。

答案 2 :(得分:0)

我有类似的错误。试图删除用户。 PG在一个帖子上提出了一个例外。 问题是我删除了id = 3的用户。但是数据库中的帖子的user_id为3.

dependent: :destroy添加到关联中为我修复了它。删除用户时,它将删除所有用户的帖子。

尝试:

has_many :comments, dependent: :destroy