简体   繁体   中英

NoMethodError: undefined method `[]' in Rails when running on Ruby 1.8.7

I create a demo rails application and generate scaffolding for the Post model. Then when I try to run rake , the integration tests fail with this error message:

NoMethodError: undefined method `[]' for #<Enumerable::Enumerator:0xb68278a8>

How can I get the demo rails application to run?

I'm trying to create a development environment to mirror an old production server, so I'm using a bunch of old versions. I'm following a tutorial on Rails 2.0.2 with Ruby 1.8.7.

The commands to create and test the demo application are as follows:

rails blog
cd blog
./script/generate scaffold Post title:string body:text
rake db:migrate
rake

I found a forum post and a blog post that describe this as a bug in Ruby 1.8.7. They suggest similar fixes, so I created a new file blog/config/initializers/ruby187_compat.rb with the following code:

unless '1.9'.respond_to?(:force_encoding) 
    String.class_eval do 
        begin 
            remove_method :chars 
        rescue NameError 
            # OK
        end 
    end 
end

Now the tests pass.

It looks like the String class in Ruby 1.8.7 got a new method chars() that somehow calls the force_encoding() method , which wasn't introduced until Ruby 1.9.1. I guess some code got incorrectly backported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM