1

我正在做 Rails 的入门课程,到目前为止已经完成了以下内容

  1. 制作了一个新的 rails 应用程序:( rails new shovell“铲子”是应用程序的名称)
  2. 安装了所有的宝石,什么不是:bundle install
  3. 生成了一个模型rails generate model Story name link
  4. 生成了一个控制器rails generate controller Stories index

现在,当我指向 时http://localhost:3000/stories,我收到一条错误消息“路由错误没有路由匹配 [GET]”/stories“”

以下是我的routes.rb

Shovell::Application.routes.draw do
  get "stories/index"
# a bunch of comments
end

所以我不知道我做错了什么,为什么它没有显示默认的欢迎消息,而是给我一个错误。谢谢你的帮助!

4

2 回答 2

2

但是,如果您这样做:

http://localhost:3000/stories/index

您可能会得到该页面,尽管这不是 Rails 方式。

首先,阅读并理解Rails 路由指南

然后为了修复您的代码,您可以在路由上编写

Shovell::Aplication.routes.draw do
  resources :stories
end

或者,如果您想要自定义路线而不是休息资源

Shovel::Application.routes.draw do
  match "stores", to: "my_controller#my_action"
end

您还可以命名自定义路线

Shovel::Application.routes.draw do
  match "stores", to: "my_controller#my_action", as: :store_index
end

因此,有了名称,您就可以在 rails 应用程序上使用路线名称

link_to("Store Index", store_index_path)
于 2012-12-06T02:31:29.983 回答
0

您已经定义了一条到 的路线/stories/index,但您没有定义一条到的路线/stories,这就是失败的原因。

你应该像这样定义这条路线:

 get '/stories', :to => "stories#index"

有关详细信息,请参阅路由指南

于 2012-12-06T02:28:55.707 回答