我在列上有一个具有单表继承的模型type
:
class Pet < ActiveRecord::Base
TYPES = [Dog, Cat, Hamster]
validates_presence_of :name
end
我想<select>
在新页面和编辑页面上提供一个下拉列表:
<% form_for @model do |f| %>
<%= f.label :name %>
<%= f.text_input :name %>
<%= f.label :type %>
<%= f.select :type, Pet::TYPES.map { |t| [t.human_name, t.to_s] } %>
<% end %>
这给了我以下错误:
ActionView::TemplateError (wrong argument type String (expected Module))
我阅读了一个使用别名的建议#type
,因为 Ruby 认为保留字与#class
. 我都试过了
class Pet < ActiveRecord::Base
...
alias_attribute :klass, :type
end
和
class Pet < ActiveRecord::Base
...
def klass
self.type
end
def klass=(k)
self.type = k
end
end
都没有奏效。有什么建议么?奇怪的是,它在我的机器上运行良好(RVM 上的 MRI 1.8.6),但在登台服务器上失败(MRI 1.8.7 不在 RVM 上)。