When I create a fresh app with rails and sqlite and start the server and to to the index action for the controller, it does no throw a nil error even when no record has been created. But I do thesame with mongoid and navigate to the index action, I get undefined method `to_sym' for nil:NilClass. In the controller index action, I tried using Message.all.to_a but the error was still thrown and I tried Message.all.entries but the error is still raised.
For I instance, I created a fresh Rails-4.1.0.rc1 app with Mongoid-4 because I am pointing to mongoid master on github and the app uses *ruby 2.1.0p0 *
I then used the scaffold generator to a create a model with just one field as shown below:
rails g scaffold message body:text
Now when I start the rails server and navaigate to **localhost:3000/messages, I get:
ActionView::Template::Error (undefined method `to_sym' for nil:NilClass):
9: </thead>
10:
11: <tbody>
12: <% @messages.each do |message| %>
13: <tr>
14: <td><%= message.body %></td>
15: <td><%= link_to 'Show', message %></td>
app/views/messages/index.html.erb:12:in `_app_views_messages_index_html_erb__250078575__604951838'
Here is the index.html.erb:
<% @messages.each do |message| %>
<tr>
<td><%= message.body %></td>
<td><%= link_to 'Show', message %></td>
<td><%= link_to 'Edit', edit_message_path(message) %></td>
<td><%= link_to 'Destroy', message, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
here is the model
class Message
include Mongoid::Document
field :body, type: String
end
The controller:
class MessagesController < ApplicationController
def index
@messages = Message.all
end
end
The route
resources :messages