undefined method `write_inheritable_attribute' for Rails::Generator::Base:Class (NoMethodError)

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

I am working on rails, suddenly rails server stops working. It's working fine and I have used following command [platform windows 7]

gem install -v=2.3.5 rails 

And when I use rails s, it's giving the following error:

c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator/options.rb:32:in `default_options': undefined method `write_inheritable_attribute' for Rails::Generator::Base:Class (NoMethodError)     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator/base.rb:90:in `'     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator/base.rb:85:in `'     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator/base.rb:48:in `'     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator/base.rb:6:in `'     from c:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'     from c:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/lib/rails_generator.rb:34:in `'     from c:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'     from c:/Ruby193/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:55:in `require'     from c:/Ruby193/lib/ruby/gems/1.9.1/gems/rails-2.3.5/bin/rails:14:in `'     from c:/Ruby193/bin/rails:23:in `load'     from c:/Ruby193/bin/rails:23:in `
' $ rails -v Rails 2.3.5 $ ruby -v ruby 1.9.3p194 (2012-04-20) [i386-mingw32]

回答1:

I had the same problem, but it was the result of having the gem rmagick.

I ran the following:

gem uninstall rmagick  gem cleanup  

And then re-ran bundle in my stack and it worked for me again.



回答2:

I have solved this issue. Use following command and problem gone

 gem install rails 

This will install updated rails and all thing working find again.



回答3:

Running bundle exec rails s worked for me.

My project was using Rails 3.x but when I ran rails -v I got 2.3.17 which is the version used by a project I worked on just before. Somehow installing this version must have be set as "default".

Could someone explain this ?



回答4:

if you are using latest version of rails 3 or rails 4 then use class_attributes instead of write_inheritable_attribute, i.e.

class_attribute :variable_name self.variable_name = value 

instead of

write_inheritable_attribute(:variable_name, value) 


回答5:

I had this issue, and it happens that I was in using the wrong ruby and gemset in rvm.



回答6:

(@jeremy-f and @alex-villa are on the right track to identifying the problem itself.)

The problem here is that the rails ... command in Rails version 2 is a generator, equivalent to rails new ... in Rails 3+. So rails s is a Rails 2 command to generate a new Rails 2 app called "s" (whereas most of us when we write rails s intend the Rails 3 usage, i.e. start a server!)

If you install a gem, it will usually install its binary in the path, so you should be able to cause this error by doing gem install rails -v 2.3.15 then rails s as the OP says.

Three ways to fix it:

  • gem install rails to install a newer version of Rails (you may have to specify a version >= 3.0)
  • Run the app with bundle exec rails s (only if you have Rails 3 in your Gemfile)
  • The usual way to run a Rails 2 project is simply script/server


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!