视图中未初始化的常量

时间:2013-09-12 00:04:48

标签: ruby-on-rails

我有两种模式:

发表:

class Post < ActiveRecord::Base 
    has_many :exes
end

埃克:

class Exe < ActiveRecord::Base
    belongs_to :post
end

我在http://localhost:3000/posts/index的观点中得到的是:

NameError in Posts#index
uninitialized constant Post::Ex

出于某种原因它只是Ex

行ruby的代码抱怨<% post.exes.each do |exe| %>看起来对我来说正确。

所以我真的不知道为什么会这样。如果还检查了以下内容,因为我认为这可能是原因,但不是:

2.0.0-p247 :004 > ActiveSupport::Inflector.pluralize('Exe')
 => "Exes" 
2.0.0-p247 :005 > ActiveSupport::Inflector.singularize('Exe')
 => "Exe" 

提前致谢!

2 个答案:

答案 0 :(得分:2)

你的问题是ActiveSupport :: Inflector假设一个以复数形式结尾的'xes'的单词必须以单数形式以'x'结尾。 See here有关自定义复数的帮助。

更新:不知怎的,我错过了你问题的最后一部分。你说你试过了:

> ActiveSupport::Inflector.singularize('Exe')

但是你试过了吗?

> ActiveSupport::Inflector.singularize('Exes')

答案 1 :(得分:1)

在项目的inflections初始化程序中为此特定字符串定义变形器:

# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'Exe', 'Exes'
end

请记住,在更改生效之前,您需要重新启动服务器。

相关问题