SitePoint Sponsor

User Tag List

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

    Question 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.

  2. #2
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    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:
    Code:
    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

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

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Akron, OH, USA
    Posts
    106
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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:

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


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
  •