Reputation:
I set up a new Rails application by following these instructions. I generated a new controller and added resources :tickets
to the routes file.
Hexapoda::Application.routes.draw do
resources :tickets
end
This is the controller (`/app/controllers/tickets_controller.rb').
class TicketsController < ApplicationController
def index
@tickets = Ticket.all
end
end
I then added a new model Ticket
in /app/models/ticket.rb
.
class Ticket
include MongoMapper::Document
key :summary, String, :required => true
end
Here's the view (/app/views/index.html.erb
):
<h1>Tickets#index</h1>
<p>Find me in app/views/tickets/index.html.erb</p>
Now when I go to /tickets
in my browser, I get an error message.
NoMethodError in TicketsController#index
undefined method `key?' for nil:NilClass
I have no idea what's going on. What could be the problem? I'm using Rails 3.2.5 and MongoMapper 0.11.1.
Upvotes: 4
Views: 3416
Reputation: 7379
This was giving me quite a bit of headache. I don't have the mongo_mapper gem installed, but restarting the rails server fixed the problem for me.
Upvotes: 0
Reputation: 1137
Jesse Wolgamott solution above me should work, if it doesn't, you may have something else wrong that is very simple.
For me, this error:
undefined method `key?' for nil:NilClass
happened because a model file that the controller was calling and displaying in the view had a simple typo in it. Make sure that when you use attr_accessible that you don't forget any of the commas between things. My issue came from using having new line characters like so:
attr_accessible :address_1, :address_2, :city <= COMMA NEEDED :country, :latitude, :longitude, :state, :zip
Be sure to do what Jesse Wolgamott above me said to do and if that doesn't work or if you are already running that version already, check your model files for typos.
Upvotes: 2
Reputation: 40277
You need the latest MonoMapper from Master:
gem 'mongo_mapper', github: "jnunemaker/mongomapper"
And run bundle
Explanation: Rails 3.2.4 added a accessible_attributes method to ActiveModel, but MongoMapper already had this; so they were clobbering each other.
MM issue: Issue 419
MM commit that fixes: 4d35c67
Upvotes: 6