0

I am getting this error from heroku logs. My app is using mongoid and MongoHQ database. It runs fine on my local machine.

2013-05-29T12:08:04.219051+00:00 app[web.1]:   Rendered posts/index.html.erb within layouts/application (7.5ms)
2013-05-29T12:08:04.219266+00:00 app[web.1]: Completed 500 Internal Server Error in 8ms
2013-05-29T12:08:04.221331+00:00 app[web.1]: 
2013-05-29T12:08:04.221331+00:00 app[web.1]: ActionView::Template::Error (undefined method `[]' for nil:NilClass):
2013-05-29T12:08:04.221331+00:00 app[web.1]:     12:     <th class="span1"></th>
2013-05-29T12:08:04.221331+00:00 app[web.1]:     13:   </tr>
2013-05-29T12:08:04.221331+00:00 app[web.1]:     14: 
2013-05-29T12:08:04.221331+00:00 app[web.1]:     15: <% @posts.each do |post| %>
2013-05-29T12:08:04.221331+00:00 app[web.1]:     16:   <tr class="row-fluid">
2013-05-29T12:08:04.221331+00:00 app[web.1]:     17:     <td class="span2"><%= post.title %></td>
2013-05-29T12:08:04.221331+00:00 app[web.1]:     18:     <td class="span1"><%= post.post_date %></td>
2013-05-29T12:08:04.221331+00:00 app[web.1]:   app/views/posts/index.html.erb:15:in `_app_views_posts_index_html_erb___2127098936622691725_37166920'
2013-05-29T12:08:04.221586+00:00 app[web.1]:   app/controllers/posts_controller.rb:12:in `index'

Do I have to do anything to MongoHQ before using it?

This is the code for index action:

  def index
    t = params[:t].to_i
    if t
      @posts = Post.where(:created_at.gt => t).asc(:created_at)
    else
      @posts = Post.all
    end

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @posts }
    end
  end
harinsa
  • 3,176
  • 5
  • 33
  • 53
  • check @posts variable if it is nil – Rubyman May 29 '13 at 12:35
  • How do I do that? I have accessed heroku rails console and do `Post.all.count` and `Post.all.entries` and both give me `undefined method '[]' for nil:NilClass` – harinsa May 29 '13 at 12:39
  • what does Post.all gives you? you don't have records in posts table hence it is giving this error – Rubyman May 29 '13 at 12:45
  • Post.all gives a Mongoid Criteria, you have to call Post.all.entries to give all the entries. It should be empty because this is the first time running the app. If I run my app locally, I don't have to do anything with mongodb (no rake db:migrate and such). I just have to run it then enter the app and create an entry. Don't have this problem. – harinsa May 29 '13 at 12:56
  • ok. see if for loop instead of each is working and paste code from index method of posts controller – Rubyman May 29 '13 at 13:05
  • @Rubyman for loop doesn't work, I have also tried to remove code I added in the index method, doesn't work either. I have posted the index method above. – harinsa May 29 '13 at 13:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30836/discussion-between-rubyman-and-doog) – Rubyman May 29 '13 at 13:23
  • did you see, that the error is actually in the template? `app/views/posts/index.html.erb:15` – phoet May 29 '13 at 15:12

0 Answers0