Syntax error when creating a Rails model - ruby-on-rails

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:
/home/XX/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:234:in `load': /home/XX/myapp/config/initializers/session_store.rb:3: syntax error, unexpected ':', expecting $end (SyntaxError)
...sion_store :cookie_store, key: '_myapp_session'
why?
my session_store.rb
Myapp::Application.config.session_store :cookie_store, key: '_myapp_session'
What's wrong with my session_store.rb?

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.

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

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

I was able to resolve this by adding an .rvmrc file to my application.
echo "rvm ruby-2.0.0#rails32 --create" > .rvmrc

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

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

Related

I deleted some old versions of gems and now rails is broken

So long story short I was running out of room on my dual booted ubuntu laptop. I decided to delete some older versions of gems to make some room which worked. Great, except now I can't get rails to work on my machine anymore - rails console and rails server is failing. This is the error I see when I type in rails s;
jmtoporek#jmtoporek-laptop:~/rails/pskr2$ rails s
/home/jmtoporek/.rvm/gems/ruby-1.9.2-p180/gems/hpricot-0.8.6/lib/fast_xs.so: [BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
/usr/bin/rails: line 104: 4807 Aborted /usr/bin/ruby /usr/share/rails/railties/bin/rails ${OVERWRITE_OPTION} "${RAILS_PKG_DESTINATION}" "${INTERNAL_OPTIONS}" -d $DATABASE
ln: target `/home/jmtoporek/rails/pskr2/s/vendor/' is not a directory: No such file or directory
ln: creating symbolic link `/home/jmtoporek/rails/pskr2/s/vendor/rails': No such file or directory
ln: creating symbolic link `/home/jmtoporek/rails/pskr2/s/doc/api': No such file or directory
I suppose reinstalling rails should be enough, because it installs several gems to run properly. Simply run gem install rails and see if it works again.
Also, if you use RVM, you can use a different installation executing something like this:
rvm use ruby-1.9.3-p286#rails328 --create --default
..and then, installing rails again:
gem install rails
Just remember this last solution requires you to install all other gems as well.
I reinstalled railties - despite the fact that the gem was already installed something must have gotten messed up. I reinstalled railties, which fixed the issue. I wish I had looked closer at the error message. It appears that there is a copy of railties inside the rails gem directory.

Rails Server Error: Ruby version is 1.8.7, but your Gemfile specified 1.9.3

I entered a existing ruby application, and type:
$ rails s
wanted to start rails server here.
but it said:
Your Ruby version is 1.8.7, but your Gemfile specified 1.9.3
Actually, I had a 1.8.7, but I deleted it. And if I do:
$ ruby -v
it said:
ruby 1.9.3p286 (2012-10-12 revision 37165) [x86_64-darwin11.4.2]
So I don't know how can I fix it. Can you give me a help?
If you are using rvm, run this:
$ rvm use 1.9.3
try using bundler
bundle exec rails s
I had similar problem:
$ bundle install
Your Ruby version is 2.1.0, but your Gemfile specified 1.9.3
but:
$ ruby -v
1.9.3-p484
$ which ruby
/home/malo/.rvm/rubies/ruby-1.9.3-p484/bin/ruby
I've found five answers: 1, 2, 3, 4, 5. Also it was open issue on github. However, I've resolved the problem as follows:
Got path to my bundler:
$ which bundle
/home/malo/.rvm/gems/ruby-1.9.3-p484#global/bin/bundle
Opened it to edit (or just cat it), and saw that it has invalid link to ruby in the first line:
$ cat $(which bundler)|head -n 1
#!/home/malo/.rvm/rubies/ruby-2.1.0/bin/ruby
Then I get the path to current valid ruby, and just replaced that invalid with it:
$ which ruby
/home/malo/.rvm/rubies/ruby-1.9.3-p484/bin/ruby
Of course you can also try replace it with the common form:
#!/usr/bin/env ruby
This should pick up the currently used ruby version.
I found out the reason I was getting this error was that I was shelling out to a Heroku command line program inside of my configuration files and Heroku Toolbelt comes with it's own version of Ruby.
The two solutions to that problem are to either not shell out to Heroku or use a Bundler.with_clean_env block instead of the backticks to shell out the heroku command.
Please try this:
1. Open your gemfile
2. Specify rails version
3. Run bundle update
4. Run rails server - rails s
Every now and then this happens with me. However I often don't like switching ruby versions here and there. So instead what I do is I just go to the Gemfile and switch the ruby version to the one that I am using. Doing this allows me to fire up my server and keep working on things.
so for instance, right now for the app i'm working on, my Gemfile is at
ruby ENV["CUSTOM_RUBY_VERSION"] || "2.1.6"
and I would just alter it to
ruby ENV["CUSTOM_RUBY_VERSION"] || "1.9.3"

After upgrading Ruby1.9.2 from p180 to p290, libruby dynamic library path isn't updated

I used RVM to upgrade Ruby 1.9.2 from patch level p180 to p290:
rvm upgrade 1.9.2-p180 1.9.2-p290
Then, I used these commands to update my Rails gem and other gems
gem install rails 3.0.5
gem update
Everything seems to be fine; rvm info shows all Ruby binaries and gems have been moved to the correct p290 path (~/.rvm/*/ruby-1.9.2-p290/*).
However, when I go to my Rails application directory and issue the command rails console, I get the error message saying a gem (activesupport-3.0.5) cannot load the libruby.1.9.1.dylib file.
10:30 AM ~/Development/rails_projects/my_app_0515 $ rails console
/Users/whk/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.5/lib/active_support/dependencies.rb:239:in `require': dlopen(/Users/whk/.rvm/gems/ruby-1.9.2-p290/gems/serialport-1.0.4/lib/serialport.bundle, 9): Library not loaded: /Users/whk/.rvm/rubies/ruby-1.9.2-p180/lib/libruby.1.9.1.dylib (LoadError)
Referenced from: /Users/whk/.rvm/gems/ruby-1.9.2-p290/gems/serialport-1.0.4/lib/serialport.bundle
Reason: image not found - /Users/whk/.rvm/gems/ruby-1.9.2-p290/gems/serialport-1.0.4/lib/serialport.bundle
. . .
Rails can't find the dylib file in ~/.rvm/rubies/ruby-1.9.2-p180/lib, because the p180 path no longer exists, but the file is in ~/.rvm/rubies/ruby-1.9.2-p290/lib.
From a separate StackOverflow post, I found a workaround is adding this line to .bashrc
export DYLD_LIBRARY_PATH="/Users/whk/.rvm/rubies/ruby-1.9.2-p290/lib:$DYLD_LIBRARY_PATH"
However, I want to understand why the rvm ruby upgrade doesn't take care of the lib path change? Does anyone know a cleaner solution -- one that removes the p180 path from where it is configured?
Here are my environment:
Mac OS X 10.6.6 (Snow Leopard)
rvm 1.8.4
ruby 1.9.2p290
Rails 3.0.5
Thanks!
the problem was in gems native extension - their were nto rebuild during rvm upgrade 1.9.2-p180 1.9.2-p290 - next time please have closer look on the output ... it should give your information what's wrong.
as for this particular use case it shoudl be enough to reinstall the given gem:
gem install serialport -v 1.0.4
... not sure if it should be uninstalled first

Problems getting Rails running on Mac OSX Snow Leopard

Update and Solution
I finally got everything working after
1. Uninstalling everything ruby-related in /usr/local/... as well as uninstalling all versions of rails.
2. Installing RVM as a standard user -> i.e. installed in my home directory and NOT in /usr/local as root
3. rvm install 1.8.7 and set as default
Now everything seems to be working fine.
My conclusion after days of googling and reading about others' solutions is that Snow Leopard just doesn't handle the rails dev environment well unless you sandbox it through RVM in a local director. I resisted going to RVM because I don't have a need to manage multiple ruby versions.
Some of the responses below along with my code excerpts on pastie may provide some helpful advice for others trying to troubleshoot. In particular from #fl00r and #Kelvin:
run which -a for gem, bundle, rails, rake, etc. to see the available versions. Check to see which versions of ruby are being called with head -1 on each.
Original Below
Pretty much at my wit's end after a few days of Googling, uninstalling, and re-installing. I'm trying to get rails running on Mac OS X 10.6.7. I followed the Hivelogic Post on this topic. Apologize for the length of this question.
Has anyone followed a step-by-step uninstall/reinstall process for getting this working? Or a link to advice on troubleshooting? Should I clean out everything following this advice from Chad Wooley and migrate to RVM?
The common theme has been errors related to being unable to find gems even though they are installed. For instance, trying to create a new rails app:
kevindewalt#new-host-4:~/Documents$ $ rails new blog
You don't have i18n installed in your application. Please add it to your Gemfile and run bundle install
Or an existing app I have running on another machine after running bundle install:
kevindewalt#new-host-4:~/Documents/ClaimAway$ (master) $ rake db:setup
(in /Users/kevindewalt/Documents/ClaimAway)
Could not find i18n-0.4.2 in any of the sources
Try running `bundle install`.
kevindewalt#new-host-4:~/Documents/ClaimAway$ (master) $ rails s
/usr/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_dispatch.rb:35:in `require': no such file to load --
-
kevindewalt#new-host-4:~/Documents$ $ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.7.2
- RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.7.0]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-10
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- /Users/kevindewalt/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
-
kevindewalt#new-host-4:~/Documents$ $ echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/opt/local/bin:/opt/local/sbin:/usr/local/git/bin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
-
kevindewalt#new-host-4:~/Documents$ $ which -a ruby
/usr/local/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
/usr/local/bin/ruby
add to Gemfile
gem 'i18n'
Actually you should specify exact version also:
gem 'i18n', '0.4.2'
then run bundle install. But as far your i18n gem will be locked try this:
bundle update i18n
That is all what is written in your error.
rvm is probably your best bet to prevent problems like this. But it might be overkill if you actually don't need multiple ruby version or gemsets.
Let me try to diagnose the issue. If we can solve it, then you don't have to jump through the rvm hoops (unless you want to of course).
I suspect that when you run some of these ruby-based scripts like 'gem', 'bundle', 'rails', and 'rake' the shebang line of these scripts is pointing to a ruby installation that you didn't expect.
Here's my usual checklist of diagnosing gem issues like this.
Run "which -a gem". Do you see multiple unique locations? If so, gem list using the full path to gem. E.g. "/usr/bin/gem list" and "/usr/local/bin/gem list". My guess is that you'll see that one of the lists has i18n and the other doesn't.
Run "which" on rails, bundler, and gem. Run "head -1" on each of those paths. Does the ruby path match in all of them?
My guess is that the "rails" in your PATH is not using the same ruby as the "gem" in your PATH. Maybe you installed it with the preinstalled 'gem' command so it's pointing to the system ruby's gem directory rather than /usr/local. Try reinstalling rails, then closing and reopening your terminal. Then use the "which" and "head -1" above to make sure "rails" is using the same ruby as "gem" is.
Another suspect is the "sudo" command. You may also want to run the "which" command via sudo, e.g. sudo sh -c 'which gem'. It's possible that sudo is running one of the preinstalled scripts.
If in doubt, you could run: "sudo /usr/local/bin/gem install rails" so you know for sure which gem command you're using.
I would do a couple of things.
Start by uninstalling Rails >= 3 from your current systems gem installation. This includes all Rails dependencies. You should be able to boot < Rails3 projects using your normal gem installation now.
Next up install rvm and use rvm to install Ruby 1.9.2 and Rails3. Use the 1.9.2 rvm to work with your Rails3 projects. Part of rails3 and rails2 don't work well together so you should always try to seperate them imho.
See this stack question for step by step instructions for installing rails using rvm. This is the most tried and true method I've found:
Uninstall Ruby on Rails on Mac OS X 10.6

Ruby on Rails undefined method `camelize' for "app":String

I just tried to play with Ruby on Rails on Snow Leopard.
I was following this tutorial:
http://developer.apple.com/tools/rubyonrails.html
Whatever I try to do with rails I end up with:
MacBook-Pro-lm:~ lukasz$ rails blog
**undefined method `camelize' for "app":String**
MacBook-Pro-lm:~ lukasz$ rails --help
**undefined method `camelize' for "app":String**
Really basic things... however - verbosing rails works:
MacBook-Pro-lm:bin lukasz$ rails -v
Rails 2.3.5
MacBook-Pro-lm:~ lukasz$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10]
MacBook-Pro-lm:bin lukasz$ gem -v
1.3.7
So it seems I have installed and upgarded ruby (using macports), rubygems and rails successfully (according to terminal). There are also few other packages installed to support we development like passenger and mysql5, etc...
I can find ruby installed in the
/opt/local/bin directory. My environment PATH variable is:
PATH=/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
The is something fishy going on. It must be some stupid basic problem. Google says that there could be a problem with ruby-iconv library but I can not find/install this package using port or gem commands.
Plese help.... what am I missing here?
You might want to try to check where the gem was installed:
gem list -d rails
Compare the gem installation directory with other gems that rails needs. It seems to me that it is a ruby gems issue. Make sure all the gems was installed with macports version of rubygems. It is not possible to mix and match.
Hopefully it helps.

Resources