服务器说没有定义路由,即使它们是

Server says no routes are defined even though they are

我在routes.rb

中定义了三个路由
Rails.application.routes.draw do

  root to: 'pages#lottery'

  get 'pages/about'
  get 'pages/contact'
  get 'pages/lottery'

end

当我在命令行中 运行 "rake routes" 时,我得到以下信息:

      Prefix Verb URI Pattern              Controller#Action
         root GET  /                        pages#lottery
  pages_about GET  /pages/about(.:format)   pages#about
pages_contact GET  /pages/contact(.:format) pages#contact
pages_lottery GET  /pages/lottery(.:format) pages#lottery

但是当我到达 localhost:3000/pages/contact 时,我得到了错误:

"No route matches [GET] "/pages/contact.html.erb"

还有"You don't have any routes defined!"

有人知道这个问题吗?

你不应该去 URL /pages/contact.html.erb,它应该只是 /pages/contact.html.

Rails 提供了很好的助手,可以轻松获得正确的路径,例如,pages_contact_path(来自您的 rake routes)。

您的 pages_controller.rb 应包含三种基于所列路线的方法。

pages_controller.rb

class PagesController < ApplicationController
  def lottery
  end

  def about
  end

  def contact
  end
end

views/pages/contact.html.erb

<p>Hello World</p>

启动您的 rails 服务器 - rails s 并导航至 localhost:3000/pages/contact,您应该会看到 Hello World

呃菜鸟错误。我在命令行中打开了两个选项卡。我的 rails s 页面位于不同的目录中。谢谢大家的帮助,感激不尽