SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Simply Rails 2 - belongs_to Clause - page 204

    I'm following Simply Rails 2 and I am stuck at page 204

    I modified the vote.rb file as follows:

    class Vote < ActiveRecord::Base
    belongs_to :story
    end
    then going over to the console I typed the following command:

    >> v= Vote.find( :first)
    and this was the reply from the console:

    => #<Vote id: 1, story_id: 1, created_at: "2009-06-17 19:20:11", updated_at: "2009-06-17 19:20:11">
    So far so good. So proceeding to the next step I typed the following command in the console:

    >> v.story
    and this is the reply from the console:

    NoMethodError: undefined method 'story' for #<Vote:0x3245524>
    from C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/attribute_methods.rb:205:in 'method_missing'
    from (irb):9

    So using page 162 as a guide (because there was a NoMethodError in Stories#new), the book said the problem is:
    we haven't declared Story as a resource anywhere.
    Again, the book said that:
    resources in Rails are declared in the file responsible for the Routing Configuration, config/routes.rb
    My routes.rb looks like this:

    ActionController::Routing::Routes.draw do |map|
    map.resources :stories
    map.connect ':controller/:action/:id'
    map.connect ':controller/:action/:id.:format'
    end
    So, I tried to add the following line to the routes.db file:
    map.resources : votes

    And this didn't solve the problem.

    So, what exactly is the solution to this problem?

    I tried searching for a solution on Google, also on Google blog search. Is there a site that anyone would recommend for information on dealing with Ruby errors?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,053
    Mentioned
    187 Post(s)
    Tagged
    2 Thread(s)
    Dealing with errors is something I'd like to be better at too. So I'm looking forward to hearing from others.

    I guess if you're only on page 204, then this from page 218 isn't the problem
    Code Ruby:
    ActionController::Routing::Routes.draw do |map|
     map.resources :stories, :has_many => :votes
    ......

    Back on page 204, it mentions needing to reload the console after making changes, did you try
    Code:
    >> reload!
    Reloading...
    => true

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    It was the reload!

    Thanks again for the help.


Tags for this Thread

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
  •