Search code examples
ruby-on-railsrubymongodbactioncontroller

Action Controller: Exception Caught [NoMethodError in Home#index]


I am trying to set up a local development environment for a ROR app that has been passed down to me from a previous developer. I am new to ROR and Ruby.

First I run bundle install with no problems, then mongod. When i run rails s the server starts, but it outputs host is not a valid option for Mongo::Connection.

When I try to go to http://0.0.0.0:3000 an error message appears. NoMethodError in Home#index

The error points to the following path:

Showing /Users/ryansnyder/Rails/myAppX/app/views/home/index.html.haml where line #12 raised:

undefined method 'url' for nil:NilClass

Extracted Source (around line #12):

9:     %h2 Asset Protection
10:     = link_to image_tag('asset-protection-guide.png'), 'http://myAppX.s3.amazonaws.com/assets/downloads/myAppX-AssetProtectionGuide.pdf', :title => 'Asset Protection Guide', :alt => 'Download our Asset Protection Guide', :target => '_blank'
11:   %section#graphs
12:     = link_to image_tag(@chart.url, :alt => @chart.alt), eval(@chart.link), :title => @chart.title
13: 
14: %section#recent-happenings
15:   %section#upcoming-events

I commented out that line in the .haml file, and refreshed http://0.0.0.0:3000 and received another undefined method 'question' for nil:NilClass error. I assume that this is either a database or error handling issue. But I am only guessing.

EDIT: I don't know if this helps, but when running rake test i receive the following output.

host is not a valid option for Mongo::Connection
/Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.19/lib/active_support/dependencies.rb:242:in `require': no such file to load -- turn (LoadError)
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.19/lib/active_support/dependencies.rb:242:in `block in require'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.19/lib/active_support/dependencies.rb:227:in `load_dependency'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.19/lib/active_support/dependencies.rb:242:in `require'
    from /Users/ryansnyder/Rails/alerstallings-master/test/test_helper.rb:4:in `<top (required)>'
    from /Users/ryansnyder/Rails/alerstallings-master/test/functional/lawyers_controller_test.rb:1:in `require'
    from /Users/ryansnyder/Rails/alerstallings-master/test/functional/lawyers_controller_test.rb:1:in `<top (required)>'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:10:in `require'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `each'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:9:in `block in <main>'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `select'
    from /Users/ryansnyder/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.0.3/lib/rake/rake_test_loader.rb:4:in `<main>'
Errors running test:functionals!

EDIT 2: mondoid.yml

defaults: &defaults
  host: localhost
  # slaves:
  #   - host: slave1.local
  #     port: 27018
  #   - host: slave2.local
  #     port: 27019

development:
  <<: *defaults
  database: alerstallings_development

test:
  <<: *defaults
  database: alerstallings_test

staging:
  uri: <%= ENV['MONGOHQ_URL'] %>

# set these environment variables on your prod server
production:
  uri: <%= ENV['MONGOHQ_URL'] %>

Any help would be appreciated.


Solution

  • You need to check that the object you're getting back is not nil. You should add some error handling and check that your database is returning the appropriate objects. A quick fix would be to conditionally run your code:

    unless @chart.nil?
      # Your previous code here
    end