使用模型名称中的大写字母覆盖 Rails 控制器路由 [英] Override Rails controller routing with capital letters in model name

查看:25
本文介绍了使用模型名称中的大写字母覆盖 Rails 控制器路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 rails 中创建一个模型:

I want to create a model in rails:

rails generate model ABCThing

所以这将创建一个表,abc_things.伟大的.问题出在控制器和路由上.我希望我的控制器是:

So this will create a table, abc_things. Great. The problem comes with the controller and routing. I want my controller to be:

class ABCThingsController < ApplicationController
end

然而,在加入routes.rb

resources :abc_things, :only => [:index]

并创建相应的 index 视图,我在浏览器中收到以下错误:

and creating the corresponding index view, i get the following error in the browser:

Expected /app/controllers/abc_things_controller.rb to define AbcThingsController

问题很容易看出("ABCThings".tableize.classify => "AbcThing"),但我不太确定如何解决它.我想覆盖从视图到控制器的 rails 默认路由,但我不确定如何.

The problem is easy to see ("ABCThings".tableize.classify => "AbcThing"), but i'm not so sure how to fix it. I want to override rails default routing from the view to the controller, but am not sure how.

希望得到任何帮助(以及更好的问题标题的建议!)

Would appreciate any help (and suggestions for a better question title!)

推荐答案

我遇到了这个问题,并且在尝试了上述所有解决方案之后;能够使用变形器解决我的问题.

I had this issue and after trying all of the above solutions; was able to fix my problem using the inflector.

就我而言,问题是 TLA::ThingsController 被解析为 Tla::ThingsController

In my case the issue was that TLA::ThingsController was being resolved as Tla::ThingsController

将以下内容放入我的初始值设定项文件夹即可修复它

putting the following in my initializers folder fixed it

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'TLA'
end

这篇关于使用模型名称中的大写字母覆盖 Rails 控制器路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆