简体   繁体   English

用Rails路由问题

[英]Routing questions with rails

I am just starting with rails and I have a very simple case. 我只是从Rails开始,我有一个非常简单的案例。 I have a "home" controller. 我有一个“家庭”控制器。 There is one action 'index' defined in this controller. 在此控制器中定义了一个动作“索引”。 When I go to ~/home I get an error msg saying: 当我去〜/ home时,出现错误消息味精:

uninitialized constant HomesController (I noticed the singular/plural thing). uninitialized constant HomesController (我注意到单数/复数)。

That's the first thing I don't get (I thought it would automatically go to ~/home/index). 这是我第一件事(我认为它会自动转到〜/ home / index)。

Second thing, if I go to ~/home/edit (note that this action doesn't exist yet), I get also: uninitialized constant HomesController But if I go to ~/home/show (show doesn't exist as well) I get a different error message: 第二件事,如果我去~/home/edit (请注意该动作尚不存在),我也会得到: uninitialized constant HomesController但是,如果我去〜/ home / show(显示也不存在)我收到不同的错误消息:

No route matches "/home/show"

How can I get 2 differents errors for the same reason (an inexistant action). 出于相同原因(不存在的操作),我如何获得2个差值错误。 And What's the deal with this constant? 这个常量怎么处理?

Thank you 谢谢

Edit I'm running rails 3.0 编辑我正在运行Rails 3.0

Here is my routes.rb file 这是我的routes.rb文件

Topnotch::Application.routes.draw do
  resources :subscriptions
  resource :home

  get "home/index"

  get "subscriptions/index"

  root :to => "home#index"
end
  1. You must add the resource "home" to the route.rb. 您必须将资源“ home”添加到route.rb。
  2. The controllers are considered to be plural. 控制器被认为是多个。
  3. If you are new to rails, I suggest you to start using generators - just open a terminal in the project's folder and type in "script/generate scaffold home" (in rails3 it would be "rails g home") 如果您不熟悉Rails,建议您开始使用发电机-只需在项目文件夹中打开一个终端,然后输入“脚本/生成脚手架首页”(在rails3中为“ rails g home”)

Changes the root route as below:- 更改根路由,如下所示:

root :to => "homes#index". 根:to =>“ homes#index”。

You must use the plural form in the routes. 您必须在路径中使用复数形式。

Turns out, the routes were correct I was just not using them correctly ! 原来,路线是正确的,我只是没有正确使用它们! rake routes helped. rake routes帮助。

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

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