Rails 模型和控制器
我生成了一个名为“搜索”的控制器。后来我也建立了一个叫做搜索的模型。当我尝试进行搜索时,我收到一个错误,指出没有名为搜索的控制器,因此看起来模型默认情况下与复数本身进行交互。我该如何改变这种行为
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来你对 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
只需将控制器类从“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 fromsearch_controller.rb
tosearches_controller.rb
.当您分别生成控制器和模型时,您还生成了其他文件,如测试文件、视图等。所以你可能会对此感到非常头疼。
我建议在以下步骤中使用
rails destroy
命令:提交当前项目(稍后您可以从中恢复内容)
运行此命令:
从之前的提交恢复控制器和模型。
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:
Restore your controller and model from previous commit.