SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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: <h2>
    2: <span id="vote_score">
    3: Score: <%= @story.votes.size %>
    4: </span>
    5: <%= @story.name %></h2>
    6: <p>


    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!!!

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    Code:
    def show
      @story = Story.find(params[:id])
    end

  3. #3
    SitePoint Member
    Join Date
    Aug 2010
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ReggieB! I shall try this out!

  4. #4
    SitePoint Member
    Join Date
    Jun 2010
    Location
    San Jose California
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •