简体   繁体   中英

Routing Error during “Ruby on Rails-Tutorial”

It seems like some people here had this problem but I couldn't find any solution in another topic.

I am doing Chapter 3 of the Ruby on Rails-Tutorial, working on the static pages. When I want to open them on the localhost it gives me a "Routing Error" in the Browser.

My Ruby is currently on version 1.9.3. My Rails is currently on version 3.2.

I have tried:

  • restarting the server
  • saving all the files again
  • checking any issues in the static_pages_controller.rb
  • checking any issues in the routes.rb
  • checking any issues in the static_oages_spec.rb

Also there are no bugs in the HTML code of the single static page. And I can't find any more help in the tutorial, neither in other questions here on StackOverflow.


Edit:

This is the actual error message from the browser:

Routing Error

No route matches [GET] "/static_pages/home" Try running rake routes for more information on available routes.

if I go to http://localhost:3000/static_pages/home , to one of three static pages I have.

This is routes.rb:

SampleApp::Application.routes.draw do

get "static_pages/home"

get "static_pages/help"

get "static_pages/about"

end

Also, I tried " rake routes " in the terminal, too. This is the result:

home_static_pages GET    /static_pages/home(.:format)  static_pages#home
 help_static_pages GET    /static_pages/help(.:format)  static_pages#help
about_static_pages GET    /static_pages/about(.:format) static_pages#about
      static_pages POST   /static_pages(.:format)       static_pages#create
  new_static_pages GET    /static_pages/new(.:format)   static_pages#new
 edit_static_pages GET    /static_pages/edit(.:format)  static_pages#edit
                   GET    /static_pages(.:format)       static_pages#show
                   PUT    /static_pages(.:format)       static_pages#update
                   DELETE /static_pages(.:format)       static_pages#destroy

And this is the error message the server is giving me:

Started GET "/static_pages/home.html" for 127.0.0.1 at 2012-04-03 13:23:54 +0200

ActionController::RoutingError (No route matches [GET] "/static_pages/home.html"):
  actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.3) lib/rails/engine.rb:479:in `call'
  railties (3.2.3) lib/rails/application.rb:220:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

If you are using Spork you must re-run Spork so your tests will consider the changes in config files like routes.rb, that are pre-loaded with Spork, so are not automatically updated.

  1. Stop Spork (Ctrl + C)
  2. Run Spork again (bundle exec spork)

Source: this is the source of this information "after changing a file included in the prefork loading (such as routes.rb ), you will have to restart the Spork server to load the new Rails environment . If your tests are failing when you think they should be passing, quit the Spork server with Control-C and restart it

Try this :

match "/static_pages/home" => "static_pages#home", :via => :get
match "/static_pages/help" => "static_pages#help", :via => :get
match "/static_pages/about" => "static_pages#about", :via => :get

Add into routes , restart the server and refresh browser .

If you check the routes.rb in config you'll probably find that the there is a 'e' missing from /home. Add that and you are golden. Or green. Or whatever :)

检查你的http://地址,特别是如果你在localhost:3000 / path或localhost:3000 / path1 / path2等

一旦你将映射的格式从get更改为match你将不再需要static_pages,只需直接转到localhost:3000/pagename例如localhost:3000/about

your routes.rb file has problems, use either RESTful style:

resource :static_pages do 
  collection do 
    get :home
    get :help
    get :about
  end 
end

or the non RESTful style:

match "static_pages/home", :controller => "static_pages", :action => "home"
match "static_pages/help", :controller => "static_pages", :action => "help"
match "static_pages/about", :controller => "static_pages", :action => "about"

for more details please refer to official guide: http://guides.rubyonrails.org/routing.html

I had a few problems working through the Rails Tutorial, and it helped to be able to consult the author's GitHub repo: https://github.com/railstutorial

Find the file you're working on and compare it line by line. Or just cut and paste the full file and see if it will run, then track down your error.

From Ruby 2 to Ruby 3 there are some differences, but still the documentation for 3 is not easy to be found. There should be a tutorial only for that: practical differences between rails 2 and 3. The guide provided by downloading Ruby on rails is "The book of ruby" but it's not good anymore. It should at least contain an advice at the beginning of chapter 19.

In

config/routes.rb

uncomment

root :to => 'welcome#index'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM