views:

335

answers:

2

I am trying to test to see if a simple run of a rail application with a database will work and I am running in to an issue.

Here are the steps I am taking:

> mkdir MyApp
> cd MyApp
> rails myapp
  ...
> rake db:create
  ...
> ruby script/generate scaffold user first_name:string last_name:string active:boolean
  ...
> rake db:migrate
  ...
> ruby script/server
  ...

From here, the first time I open http://localhost:3000/users the page will open and then I click "New User". Then I get this error:

NoMethodError in Users#index
Showing app/views/layouts/users.html.erb where line #12 raised:

undefined method `^' for "7":String

RAILS_ROOT: /Users/lillq/MyApp

/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.4/lib/active_support/message_verifier.rb:46:in `block in secure_compare'
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-2.3.4/lib/active_support/message_verifier.rb:45:in `each'
... 
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-2.3.4/lib/action_view/base.rb:197:in `flash'
/Users/lillq/MyApp/app/views/layouts/users.html.erb:12:in `_run_erb_app47views47layouts47users46html46erb'
/Users/lillq/MyApp/app/controllers/users_controller.rb:7:in `index'

So, first I thought that the versions might not be compatible, but there are several questions that say that 1.9.1 and rails are compatible.

Both say that Rail and Ruby 1.9 should work.

So here are the versions that I am running:

lillq:~/MyApp > ruby --version
ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-darwin10.0.0]
lillq:~/MyApp > gem --version
1.3.5
lillq:~/MyApp > gem list

*** LOCAL GEMS ***

actionmailer (2.3.4)
actionpack (2.3.4)
activerecord (2.3.4)
activeresource (2.3.4)
activesupport (2.3.4)
mysql (2.8.1)
rack (1.0.0)
rails (2.3.4)
rake (0.8.7)
sqlite3-ruby (1.2.5)

So from what I can find online, all things are telling me that this should run. What am I missing?

+2  A: 

I had to fix this issue not too long ago myself. The patch (Jakub's not hukl's) at the beginning of this thread will fix the problem. The discussion also explains why the problem exists in the first place, a difference in behavior of how Ruby 1.9 treats bytes.

statenjason
+1  A: 

Thanks to statenjason for link to undefined method `^' for String - RoR 2.3.4 which provides the solution.

A link to the patch is here.

From this document I took the code and made the change to the file:

lib/ruby/gems/1.9.1/gems/activesupport-2.3.4/lib/active_support/message_verifier.rb

message_verifier.rb old secure_compare:

  def secure_compare(a, b)
    if a.length == b.length
      result = 0
      for i in 0..(a.length - 1)
        result |= a[i] ^ b[i]
      end
      result == 0
    else
      false
    end
  end

message_verifier.rb new secure_compare:

  def secure_compare(a, b)
    if a.respond_to?(:bytesize)
      # > 1.8.6 friendly version
      if a.bytesize == b.bytesize
        result = 0
        j = b.each_byte
        a.each_byte { |i| result |= i ^ j.next }
        result == 0
      else
        false
      end
    else
      # <= 1.8.6 friendly version
      if a.size == b.size
        result = 0
        for i in 0..(a.length - 1)
          result |= a[i] ^ b[i]
        end
        result == 0
      else
        false
      end
    end
  end

After making this change, the issue is solved.

lillq