简体   繁体   English

路由错误没有路由匹配[GET]“/ static_pages / home”,教程

[英]Routing Error No route matches [GET] “/static_pages/home”, tutorial

When I run server browser show me something like this: 当我运行服务器浏览器时显示如下:

Routing Error

No route matches [GET] "/static_pages/home"

Try running rake routes for more information on available routes. 

Rake routes shows me this: 耙路线告诉我这个:

root  /                  static_pages#home
help  /help(.:format)    static_pages#help
about  /about(.:format)   static_pages#about
contact  /contact(.:format) static_pages#contact

My routes.rb file: 我的routes.rb文件:

MyApp::Application.routes.draw do
root :to => 'static_pages#home'

match '/help',    :to => 'static_pages#help'
match '/about',   :to => 'static_pages#about'
match '/contact', :to =>'static_pages#contact'


end

Anyone got an idea? 有人有个主意吗?

There is no route set for the url '/static_pages/home' url'/ static_pages / home'没有路由集

Although root points to static_pages controller with action home, it still responds to the path '/' and not '/static_pages/home' 虽然root指向带有action home的static_pages控制器,但它仍然响应路径'/'而不是'/ static_pages / home'

If you add 如果你添加

match '/static_pages/home', :to =>'static_pages#home'

You will get the expected response for '/static_pages/home' 您将获得'/ static_pages / home'的预期响应

step 5.3.3 Rails tutorial 步骤5.3.3 Rails教程

You don't have to refresh your page 您不必刷新页面

http://localhost:3000/static_pages/home

but only change the URL by 但只能更改网址

http://localhost:3000/

because you define 'static_pages/home' as root '/'. 因为你将'static_pages / home'定义为root'/'。

For me it works 对我来说它有效

I just got the same error as szatan when I followed the Ruby on Rails tutorial. 当我遵循Ruby on Rails教程时,我得到了与szatan相同的错误。 The error is because, previously, we test URL was http://localhost:3000/static_pages/help since the action is in static_pages. 错误是因为,之前我们测试的URL是http://localhost:3000/static_pages/help因为操作是在static_pages中。 But, after changing routes.rb from 但是,在改变routes.rb之后

get 'static_pages/help' to
match '/help',    to: 'static_pages#help

The URL should changed to http://localhost:3000/help since we tell Rails server to route the path /help to static_pages#help . URL应更改为http://localhost:3000/help因为我们告诉Rails服务器将路径/help路由到static_pages#help We shouldn't expect the user to know the path /static_pages/help . 我们不应指望用户知道路径/static_pages/help

Remember to remove the "public/index.html" file. 请记住删除“public / index.html”文件。

You can define all the routes properly, but if you leave this file, you won't be routing it properly. 您可以正确定义所有路由,但如果您保留此文件,则不会正确路由它。

I had this problem when I got to this point in the tutorial. 当我到达教程中的这一点时,我遇到了这个问题。 It was resolved when I pointed the browser to "http://localhost:3000/" and reloaded. 当我将浏览器指向"http://localhost:3000/"并重新加载时,它已得到解决。

You could point to "/" or root_path in stead of '/static_pages/home' . 您可以指向"/"root_path而不是'/static_pages/home' No need to point two routes to the same place... 无需将两条路线指向同一个地方......

you should add :as keword to define XXX_path,such as root :to => 'static_pages#home' 你应该添加:作为定义XXX_path的keword,例如root:to =>'static_pages #home'

match '/', to: 'static_pages#home', :as => :home
match '/help',    to: 'static_pages#help', :as => :help
  match '/about',   to: 'static_pages#about', :as => :about
  match '/contact', to: 'static_pages#contact', :as => :contact

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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