使用资源时Rails路由错误,但现在使用GET时

时间:2022-09-08 23:47:41

I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)

我正在创建一个简单的建议框应用程序(学习Rails),当我转到我的本地计算机上运行的“/ suggestion-boxes”时,我收到以下Rails路由错误(localhost:3000)

"Routing Error

No route matches [GET] "/suggestion-boxes"

没有路线匹配[GET]“/ suggestion-boxes”

In my routes.rb file I have:

在我的routes.rb文件中,我有:

SuggestionBoxApp::Application.routes.draw do
  resources :suggestion_boxes
end

This is what I get when I run rake routes:

这是我在运行rake路线时得到的:

suggestion-box-app$ rake routes
suggestion_boxes GET    /suggestion_boxes(.:format)          suggestion_boxes#index
                POST   /suggestion_boxes(.:format)          suggestion_boxes#create
new_suggestion_box GET    /suggestion_boxes/new(.:format)      suggestion_boxes#new
edit_suggestion_box GET    /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
 suggestion_box GET    /suggestion_boxes/:id(.:format)      suggestion_boxes#show
                PUT    /suggestion_boxes/:id(.:format)      suggestion_boxes#update
                DELETE /suggestion_boxes/:id(.:format)      suggestion_boxes#destroy

However, if I modify my routes file to

但是,如果我修改我的路线文件

SuggestionBoxApp::Application.routes.draw do
  get "suggestion-boxes" => "suggestion_boxes#index"
end

Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.

然后根据我的SuggestionBoxesController中的索引操作显示页面“/ suggestion-boxes”。

I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.

我尝试重新启动我的服务器,但这没有任何影响。虽然我当然可以使用GET,但这个错误毫无意义,我想了解导致它的原因。

Any insights would be very much appreciated.

任何见解都将非常感激。

2 个解决方案

#1


1  

Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

在代码中的某个地方,您正在调用不存在的建议框控制器。你的控制器是suggestion_boxes,拼写。因此,每当你有“建议框”时,你应该用“suggestion_boxes”替换。您添加的代码创建了一个别名,该别名将'suggestion-boxes'与suggestion_boxes控制器的索引操作相匹配,因此如果这是您想要的影响,则会解析它。但只需修复拼写就可以解决问题。如果我想要更改用户看到的URL,我通常会使用第二种方法。查看路由文档以便更好地理解

#2


2  

The error is that you are not renaming the REST route, instead the controller one. Try declaring

错误是您没有重命名REST路由,而是重命名控制器路由。尝试声明

resources :suggestion_boxes, :as => "suggestion-boxes"

in your config/routes.rb file.

在你的config / routes.rb文件中。

#1


1  

Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding

在代码中的某个地方,您正在调用不存在的建议框控制器。你的控制器是suggestion_boxes,拼写。因此,每当你有“建议框”时,你应该用“suggestion_boxes”替换。您添加的代码创建了一个别名,该别名将'suggestion-boxes'与suggestion_boxes控制器的索引操作相匹配,因此如果这是您想要的影响,则会解析它。但只需修复拼写就可以解决问题。如果我想要更改用户看到的URL,我通常会使用第二种方法。查看路由文档以便更好地理解

#2


2  

The error is that you are not renaming the REST route, instead the controller one. Try declaring

错误是您没有重命名REST路由,而是重命名控制器路由。尝试声明

resources :suggestion_boxes, :as => "suggestion-boxes"

in your config/routes.rb file.

在你的config / routes.rb文件中。