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
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.
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 -> highest priority.
# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Keep in mind you can assign values other than :controller and :action
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# Sample resource route (maps HTTP verbs to controller actions automatically):
# map.resources :products
# Sample resource route with options:
# map.resources :products, :member => { :short => :get, :toggle => :post }, :collection => { :sold => :get }
# Sample resource route with sub-resources:
# map.resources :products, :has_many => [ :comments, :sales ], :has_one => :seller
# Sample resource route with more complex sub-resources
# map.resources :products do |products|
# products.resources :comments
# products.resources :sales, :collection => { :recent => :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 => "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 => :votes
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.resource :session
end
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