Simply Rails 2 Page 160 problem

Hi there,

I am new here and would appreciate any advice.

I am using the latest versions of rails / ruby / rvm etc as of about 2 days ago (when I set everything up).

I created a new view (~app/views/stories/new.html.erb) by creating a blank document placing some code in it and saving it. When I try to view it however (http://localhost:3000/stories/new) it reports a routing error.

Is there something I am missing here? Do I need to somehow report the presence of this new view? I noted that in the text the index.html.erb view (which displays correctly - although I have to actually type “~stories/index” as the url rather than it just being found at "stories/ ") is created automatically when I generated the controller. I couldn’t find any other way to generate a new view - except by using a scaffold? It’s quite frustrating as it means I can no longer continue with the tutorial in the book!

Thanks for any advice you can give me.

Well, in order to get to the bottom of your problem, post the contents of your routes.rb file, stories_controller.rb, and the whole error message.

As far as generating views, it is possible when you generate your controllers. The following link gives you a great overview of using the command line generators. http://edgeguides.rubyonrails.org/command_line.html

Thanks for taking the time to help me - here are the contents of the files you requested. Please let me know if there is anything else which may be useful.

routes.rb:

Shovell::Application.routes.draw do
  get "stories/index"

  # The priority is based upon order of creation:
  # first created -> highest priority.

  # Sample of regular route:
  #   match 'products/:id' => 'catalog#view'
  # Keep in mind you can assign values other than :controller and :action

  # Sample of named route:
  #   match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
  # This route can be invoked with purchase_url(:id => product.id)

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

  # Sample resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Sample resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Sample resource route with more complex sub-resources
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', :on => :collection
  #     end
  #   end

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

  # You can have the root of your site routed with "root"
  # just remember to delete public/index.html.
  # root :to => 'welcome#index'

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

  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id))(.:format)'
end

stories_controller.rb:

class StoriesController < ApplicationController
  def index
    @story = Story.find(:first, :order => 'RANDOM()')
  end

  def new
    @story = Story.new
  end
end

Error message:

Routing Error

No route matches [GET] “/stories/new”

Try running rake routes for more information on available routes.

Rake routes output:

stories_index GET /stories/index(.:format) stories#index

Hey mate,

In your routes.rb file you currently only have

get "stories/index"

you’ll either need to add stories/new or just add resources :stories and rake.

Yup, as t.ridge says you don’t have a route defined for your new method. You can erase most of the routes file as it is all comments. To get things working you can change your routes file to the following:


Shovell::Application.routes.draw do
  resources :stories
end