Ruby on Rails routes.rb

时间:2012-11-03 15:15:47

标签: ruby-on-rails

我陷入Rails tutorial试图配置Rails路由。

这是我的routes.rb:

SampleApp::Application.routes.draw do
  match '/', :to  => 'static_pages#home', :via => :get
  match '/help',:to => 'static_pages#help', :via => :get
  match '/about', :to => 'static_pages#about', :via => :get
  match '/contact', :to => 'static_pages#contact', :via => :get

当我尝试访问“localhost”时,我得到:

  

没有路由匹配[GET]“/ static_pages / help”尝试运行rake路由   有关可用路线的更多信息。

我也尝试过:

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

但是这会产生同样的错误。它只在我使用时有效:

get 'static_pages/about'

如何让localhost工作?

2 个答案:

答案 0 :(得分:0)

尝试将以下内容放在routes.rb

的末尾
match ':action' => 'static#:action'

/help的请求将呈现app/views/static/help.html.erb。不要忘记创建static控制器,即使它是空的。

答案 1 :(得分:0)

试试这个:

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