Chapter 7 Undefined method 'name' error

Hi guys, I am a newbie and have run into a wall in Chapter 7, Ajax and Web 2.0.
I am trying to display the page based on ID number, for example by loading http://localhost:3000/stories/2
However, I keep getting the error:

NoMethodError in Stories#show
undefined method `name’ for nil:NilClass

Extracted source (around line #1):

1: <h2><%= @story.name %></h2>
2: <p><%= link_to @story.link, @story.link %></p>
Request

Parameters:

{“id”=>“1-sitepoint-forums”}

I have been trying to find a solution on Google to no avail. I thought it might have to do with no values for ‘name’ being passed correctly or some problem with the routes. I tried playing around with both, but have not been able to find a solution.

Any help is greatly appreciated.

Hi ReggieB, I have pasted below the entire code of the config/routes.rb file…Thanks!!!

ActionController::Routing::Routes.draw do |map|
  # The priority is based upon order of creation: first created -&gt; highest priority.

  # Sample of regular route:
  #   map.connect 'products/:id', :controller =&gt; 'catalog', :action =&gt; 'view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   map.purchase 'products/:id/purchase', :controller =&gt; 'catalog', :action =&gt; 'purchase'
  # This route can be invoked with purchase_url(:id =&gt; product.id)

  # Sample resource route (maps HTTP verbs to controller actions automatically):
  #   map.resources :products

  # Sample resource route with options:
  #   map.resources :products, :member =&gt; { :short =&gt; :get, :toggle =&gt; :post }, :collection =&gt; { :sold =&gt; :get }

  # Sample resource route with sub-resources:
  #   map.resources :products, :has_many =&gt; [ :comments, :sales ], :has_one =&gt; :seller
  
  # Sample resource route with more complex sub-resources
  #   map.resources :products do |products|
  #     products.resources :comments
  #     products.resources :sales, :collection =&gt; { :recent =&gt; :get }
  #   end

  # Sample resource route within a namespace:
  #   map.namespace :admin do |admin|
  #     # Directs /admin/products/* to Admin::ProductsController (app/controllers/admin/products_controller.rb)
  #     admin.resources :products
  #   end

  # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
  map.root :controller =&gt; "stories"

  # See how all your routes lay out with "rake routes"

  # Install the default routes as the lowest priority.
  # Note: These default routes make all actions in every controller accessible via GET requests. You should
  # consider removing or commenting them out if you're using named routes and resources.
  map.resources :stories, :has_many =&gt; :votes
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.resource :session
end

Post your stories controller and we can take a look at that and maybe figure out what is wrong! :slight_smile:

Have you got RESTFUL routing enabled for this controller. Can you post your config/routes.rb file?

That looks fine.


map.resources :stories, :has_many => :votes

That line is enabling the restful charlies.

However, I think I’ve spotted the problem. It is this:


Parameters:

{"id"=>"1-sitepoint-forums"}

That should be


Parameters:

{"id"=>"1"}

I can’t see how you can get that unless you the url you send via GET is:

http://localhost:3000/stories/1-sitepoint-forums

Which doesn’t make sense.

What do you get if you enter the url:

http://localhost:3000/stories/show/1

Hi Scannon, first of all thanks very much for responding! I am really going nuts…
I have pasted the code to my story controller below of what I have done so far

class StoriesController < ApplicationController
before_filter :login_required, :only => [ :new, :create ]
def index
@story = Story.find(:first, :order => ‘RANDOM()’)
end
def new
@story = Story.new
end
def create
@story = @current_user.stories.build params[:story]
@story = Story.new(params[:story])
if @story.save
flash[:notice] = ‘Story submission succeeded’
redirect_to stories_path
else
render :action => ‘new’
end
def show
@story = Story.find(params[:id])
end

end
end

Thanks!