Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails mysql2 undefined method `accept' for nil:NilClass

I know that was a million times here but tried everything and I am still getting this error:

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

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

This is the gem list (not all):

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)

I have tried with all versions of mysql2 (from 0.2.7 to 0.3.2)

Edit (database file):

development:
  adapter: mysql2
  database: tripwall
  username: root
  password: pass
  host: localhost
  pool: 5
  timeout: 5000
like image 794
mitch Avatar asked Oct 04 '12 15:10

mitch


3 Answers

Both answers from Sean and Mitch contribute to the solution:

  • Version of mysql2 gem should be 0.3.11 and above, and
  • activerecord-mysql2-adapter should be removed from gemfile.

Wanted to make it clear that:

  1. Your gemfile should contain the following line (remove activerecord-mysql2-adapter alone):

    gem 'mysql2'

  2. gem list should output the following line:

    activerecord-mysql2-adapter (0.0.3)

    mysql2 (0.3.11)

Edit:

I also got the below error when I tried to open the index of a scaffold-generated model:

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

I had to restart the rails server; and that fixed the problem.

like image 113
Subhash Avatar answered Nov 19 '22 04:11

Subhash


I just remembered what I did to fix the error when I had it.

Just remove

activerecord-mysql2-adapter (0.0.3)

from your gemfile!

Hopefully that fixes it!

Cheers, Sean

like image 24
Sean Avatar answered Nov 19 '22 04:11

Sean


I don't know why but when I add gem "mysql2", ">= 0.3.11" and it works. I have tried gem "mysql2", '0.3.2' and that was not working. Thank you anyway.

like image 2
mitch Avatar answered Nov 19 '22 03:11

mitch