Domanda

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
È stato utile?

Soluzione 2

The bug seems to be a mongoid bug that makes it unable to work well with rails 4.1.0.rc1. I know this because followed thesame steps to create two different rails app, keeping the mongoid version constant, that is mongoid points to master. I then used rails 4.0.4 for an app and rails 4.1.0.beta2 for another. Both apps worked as expected, that is navigating to the index page does not throw an error even if there is no record in the database.

so the solution is to downgrade to an earlier version of rails.

Altri suggerimenti

Use this

<% unless @messages.nil? %>
<% @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 %>
<% end %>

It seems like @messages is set to nil. It means that Message.all is returning nil. You don't have any records in messages table.

EDIT

It seems like an issue in the rails 4.1.0.rc1 version. Try the same code with a stable build of Rails.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top