3

I am following Jumpstartlab's Blogger Rails tutorial.

I am using Ruby 2.2.1 and Rails 4.0.0.

The "undefined method 'name' for nil:NilClass" error is raised when I add

(<%= @article.comments.size %>)

to

<h3>Comments</h3>

in ~/show.html.erb.

If I swap this line with

<%= render partial: 'articles/comment', collection: @article.comments %>

I get no error and everything displays correctly (except that the header displays in the wrong spot - after the comments, rather than before).

I tried git hard reset to restore the entire comments section and redo all of it, but I got the same error.

Here is my code for ~/show.html.erb when it raises the error:

<h1><%= @article.title %></h1>
<p><%= @article.body %></p>
<h3>Comments (<%= @article.comments.size %>)</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>
<%= render partial: 'comments/form' %>
<%= link_to '<< Back to Articles List >>', articles_path %>
<%= link_to 'edit', edit_article_path(@article) %>
<%= link_to 'delete', article_path(@article), method: :delete, data: {confirm: "Really delete the article?"} %>

I am using...

articles.rb:

class Article < ActiveRecord::Base
  has_many :comments
end

comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :article
end

My fix is to swap

<h3>Comments (<%= @article.comments.size %>)</h3>

with

<%= render partial: 'articles/comment', collection: @article.comments %>

but this displays the header at the wrong place. It also doesn't make any sense to me why this works. Note that there are no errors if I do not include

(<%= @article.comments.size %>)` in `<h3>Comments

The solution to "NoMethodError undefined method `name' for nil:NilClass)" led me to check my databases for missing ids, but there were no problems there.

Community
  • 1
  • 1
  • 1
    I don't see where you call `name` on anything – Andrey Deineko Jan 23 '16 at 17:31
  • Using the order of commands you want, what happens when you add this line between the two lines in question: `p @article.comments.pluck(:id)`? It'll mess up your page a bit (it's just intended to help diagnose) but you should see an array of ids shown on the page. If it doesn't that may help us figure out what you have going on. – rdnewman Jan 23 '16 at 17:37
  • @rdnewman adding `

    <%= @article.comments.pluck(:id) %>

    ` gave me a line with [5]. Creating an additional comment for that article changed that line to [5,9]. Of course, I had to remove @article.comments.size to be able to display the pluck line.
    – Jean Merlet Jan 23 '16 at 17:46
  • @AndreyDeineko I'm fairly confident this error usually has nothing to do with calling `name` on anything, based on the solutions to this same error I've googled. – Jean Merlet Jan 23 '16 at 17:47
  • Ok, then as an experiment, change the pluck line you used from `

    <%= @article.comments.pluck(:id) %>

    ` to be `<% testx = @article.comments.size %>` (notice no "="). Then new line `

    <%= testx %>

    `. Do you see a size now?
    – rdnewman Jan 23 '16 at 20:38
  • No, this returns the same error as before. I am wondering if this is a rails bug - time to learn how to use rvm to change rails versions. – Jean Merlet Jan 24 '16 at 16:29
  • 1
    Yea, it was a rails version issue (or a ruby version issue, depending on how you look at it). My original search wasn't good enough to find this solution. Everything works as expected when upgrading to rails 4.1.2, as suggested [here](https://github.com/activescaffold/active_scaffold/issues/380). Though I believe [this](https://github.com/rails/rails/issues/18991) is a more comprehensive discussion on the problem. I don't believe these solutions are on SO, however. – Jean Merlet Jan 24 '16 at 17:30
  • 1
    Well, this seems to be the same problem as these two SO solutions: [one](http://stackoverflow.com/questions/23568084/create-with-has-many-through-association-gets-nomethoderror-undefined-method-n) and [two](http://stackoverflow.com/questions/28061409/weird-issue-has-many-through-association-in-updated-rails). However, the error message I had didn't mention ActiveRecord or has_many associations at all, so I didn't know to type that into my search. – Jean Merlet Jan 24 '16 at 18:40
  • can you paste exact error log – Mukesh Feb 04 '17 at 19:29

1 Answers1

2

You could try using .count or .length instead of .size.

<h3>Comments (<%= @article.comments.count %>)</h3>

I'm using ruby 2.3 and rails 4.0.0.

RedBassett
  • 3,469
  • 3
  • 32
  • 56
jerry_3105
  • 21
  • 2