Rails 模型和控制器

发布于 2024-12-09 20:45:04 字数 104 浏览 1 评论 0原文

我生成了一个名为“搜索”的控制器。后来我也建立了一个叫做搜索的模型。当我尝试进行搜索时,我收到一个错误,指出没有名为搜索的控制器,因此看起来模型默认情况下与复数本身进行交互。我该如何改变这种行为

I generated a controller called Search. later I built a model called search as well. When i try to make a search i get an error that says there is no controller called searches, so it looks like it the model interacts with the plural of itself by default. how do i change this behaviour

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不如归去 2024-12-16 20:45:04

听起来你对 Rails 还很陌生。这里最好的建议几乎肯定是“不要”Rails 使用大量约定来避免配置所有内容,这就是其中之一。因此,我建议更改控制器的名称,而不是尝试让 Rails 做一些不寻常的事情,

有关更多详细信息,请参阅 这个 stackoverflow 问题

Sounds like you are quite new to rails. The best advice here is almost certainly "don't" Rails uses a lot of conventions to avoid having to configure everything, and this is one of them. So I would recommend changing the name of your controller rather than trying to make rails do something out of the ordinary

for more detail see this stackoverflow question

世界如花海般美丽 2024-12-16 20:45:04

只需将控制器类从“SearchController”重命名为“SearchesController”即可。还将 apps/controllers 中的文件从 search_controller.rb 重命名为 searches_controller.rb

Just rename the controller class from 'SearchController' to 'SearchesController'. Also rename the file in apps/controllers to from search_controller.rb to searches_controller.rb.

固执像三岁 2024-12-16 20:45:04

当您分别生成控制器和模型时,您还生成了其他文件,如测试文件、视图等。所以你可能会对此感到非常头疼。

我建议在以下步骤中使用 rails destroy 命令:

提交当前项目(稍后您可以从中恢复内容)

运行此命令:

rails destroy model Search
rails destroy controller Search

从之前的提交恢复控制器和模型。

When you have generated controller and model separately, you also generated additional files like test files, views and so on. So potentially you will have big headache on this.

I would recommend to use rails destroy command in these steps:

Commit your current project (later you can recover your content from it)

Run this commands:

rails destroy model Search
rails destroy controller Search

Restore your controller and model from previous commit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文