Syntax error when creating a Rails model

后端 未结 6 1580
旧时难觅i
旧时难觅i 2020-12-19 20:10

I am creating Car model in Rails 3 by using command:

rails generate model Car name:string id_str:string

but I got the error:



        
相关标签:
6条回答
  • 2020-12-19 20:11

    I resolved this by deleting my ~/.rvm directory and reinstalling RVM.

    0 讨论(0)
  • 2020-12-19 20:12

    Well through a bit of debugging and playing around, this is what I ended up doing:

    for x in `gem list --no-versions`; do sudo gem uninstall $x; done
    

    This forces all 'old' gems to be removed. I then proceeded to verify the gem environment to confirm that gem was using the correct ruby version:

    sudo gem env
    

    The error experienced is due to the fact that rails is still trying to use ruby 1.8, which causes all sorts of issues.

    Once all of the above checks out properly do:

    sudo gem update --system
    sudo gem install rubygems-update
    sudo update_rubygems
    

    Which will make sure that you are at the latest gem version. Then:

    sudo gem install rails
    

    This will install everything you need. I ran all of the gem commands as sudo, to make sure that my root install was properly configured/setup.

    Your final gem env should look something like:

    RubyGems Environment:
      - RUBYGEMS VERSION: 1.8.13
      - RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [x86_64-darwin10.8.0]
      - INSTALLATION DIRECTORY: /usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1
      - RUBY EXECUTABLE: /usr/local/Cellar/ruby/1.9.3-p0/bin/ruby
      - EXECUTABLE DIRECTORY: /usr/local/Cellar/ruby/1.9.3-p0/bin
      - RUBYGEMS PLATFORMS:
        - ruby
        - x86_64-darwin-10
      - GEM PATHS:
         - /usr/local/Cellar/ruby/1.9.3-p0/lib/ruby/gems/1.9.1
    
    0 讨论(0)
  • 2020-12-19 20:15

    This error usually happens when you are trying to use the Ruby 1.9.2 Hash syntax with Ruby < 1.9.

    In Ruby 1.9.2 the following code works perfectly

    Myapp::Application.config.session_store :cookie_store, key: '_myapp_session'
    

    while in Ruby < 1.9 you must use

    Myapp::Application.config.session_store :cookie_store, :key => '_myapp_session'
    

    This is strange, because your stack trace references Ruby 1.9.2. Are you sure you are running the generator with Ruby 1.9.2?

    In any case, you can convert your session_store.rb file to the hash-rocket syntax. If it works, it means you are not using Ruby 1.9.2 and you pasted an invalid error message.

    0 讨论(0)
  • 2020-12-19 20:28

    I was able to resolve this by adding an .rvmrc file to my application.

    echo "rvm ruby-2.0.0@rails32 --create" > .rvmrc
    
    0 讨论(0)
  • 2020-12-19 20:35

    TL;DR: uninstall then reinstall the railties gem

    Long answer:

    In your project directory, if you type in which rails, what do you see? On mine, I saw /usr/bin/rails. This gave me a clue that it's using the wrong rails executable. If I look at my $PATH, I can see that RVM's bin path (/Users/ramon/.rvm/gems/ruby-1.9.3-p194/bin) did not contain the rails file (as to how this got deleted -- I don't have a clue). That must have been why the /usr/bin/rails got picked up by my system instead.

    Looking at the Rails source, I saw that the railties gem has the rails bin file. I uninstalled railties (gem uninstall railties), then reinstalled it again. Voila - which rails shows /Users/ramon/.rvm/gems/ruby-1.9.3-p194/bin/rails

    0 讨论(0)
  • 2020-12-19 20:35

    really strange question as error is self decribing

    you have some mess in this file, and missing end statement

    `load': /home/XX/myapp/config/initializers/session_store.rb:3
    
    0 讨论(0)
提交回复
热议问题