簡體   English   中英

Rails mysql2 undefined方法`accept'代表nil:NilClass

[英]Rails mysql2 undefined method `accept' for nil:NilClass

我知道這是一百萬次,但嘗試了一切,我仍然得到這個錯誤:

$ rake db:migrate
rake aborted!
undefined method `accept' for nil:NilClass

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

這是寶石列表(不是全部):

activemodel (3.2.8)
activerecord (3.2.8)
activerecord-mysql2-adapter (0.0.3)
builder (3.1.3, 3.0.3)
bundler (1.2.1)
mysql2 (0.3.2)

rails (3.2.8)
rails_apps_composer (2.2.10)
railties (3.2.8)
rake (0.9.2.2)
rdoc (3.12)
sqlite3 (1.3.6)
therubyracer (0.10.2)
thor (0.16.0)
tilt (1.3.3)
treetop (1.4.10)
twitter-bootstrap-rails (2.1.3)
tzinfo (0.3.33)
uglifier (1.3.0)

我嘗試過所有版本的mysql2(從0.2.7到0.3.2)

編輯(數據庫文件):

development:
  adapter: mysql2
  database: tripwall
  username: root
  password: pass
  host: localhost
  pool: 5
  timeout: 5000

Sean和Mitch的答案都有助於解決方案:

  • mysql2 gem的版本應為0.3.11及以上版本
  • 應該從gemfile中刪除activerecord-mysql2-adapter。

想要說清楚:

  1. 您的gemfile應包含以下行(僅刪除activerecord-mysql2-adapter):

    寶石'mysql2'

  2. gem list應輸出以下行:

    activerecord-mysql2-adapter(0.0.3)

    mysql2(0.3.11)

編輯:

當我嘗試打開腳手架生成的模型的索引時,我也遇到了以下錯誤:

NoMethodError (undefined method `accept' for nil:NilClass):
app/controllers/posts_controller.rb:5:in `index'

我不得不重啟rails服務器; 並解決了這個問題。

我記得當我擁有它時我做了什么來修復錯誤。

只需刪除

activerecord-mysql2-adapter(0.0.3)

來自你的gemfile!

希望能解決它!

干杯,肖恩

我不知道為什么,但當我添加寶石“mysql2”,“> = 0.3.11”,它的工作原理。 我已經嘗試過gem“mysql2”,“0.3.2”,但是沒有用。 還是非常感謝。

我建議將localhost to 127.0.0.1更改localhost to 127.0.0.1 請顯示您的模型,也許某些字段是mysql的關鍵字

請將port: 3306添加到database.yml,將127.0.0.1添加到localhost

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM