Problems with show.html.erb

Hi, I am really desperate at this point!

I am getting many errors, related to show.html.erb in the app\views\stories\ folder.

They all relate to:

NoMethodError in Stories#show

undefined method `votes’ for nil:NilClass

Extracted source (around line #3):

1:


2:
3: Score: <%= @story.votes.size %>
4:

5: <%= @story.name %>


6:

I posted a thread before, in which I mentioned that I got:

NoMethodError in Stories#show

undefined method `names’ for nil:NilClass

also present in the same file…I am losing my mind…Some help please!!!

assuming that you have a object named ‘story’ in your DB, you need to check your story controller and check if there is a appropriate method to call that object.

Thanks ReggieB! I shall try this out!

The error indicate that @story is currently a nil object. The nil Object is of the class NilClass, and this doesn’t have a names method.

The reason you are getting this is because @story is not being loaded with a Story object elsewhere in your code. For example: because @story is not being assigned in your controller. This may be because the code that assigns it is absent, or is not working as you expect.

For example, there should be something like this in the controller:


def show
  @story = Story.find(params[:id])
end

Just in case someone else comes across this, I had the same error. My solution may help you.

The instruction on p212 confused me (the code example is actually an excerpt). My mistake was to replace all the code instead of appending it. Try checking app/models/story.rb has the following code…


class Story < ActiveRecord::Base
  validates_presence_of :name, :link
  has_many :votes
  def to_param
    "#{id}-#{name.gsub(/\\W/, '-').downcase}"
  end
end

Hope this helps!

C.