【问题标题】:Rails 3 uninitialized constant error, has_many and belongs_toRails 3未初始化常量错误,has_many和belongs_to
【发布时间】:2012-08-20 16:12:10
【问题描述】:

我在两个活动记录之间创建了一个简单的 has_many 和 belongs_to 关系,当我尝试创建未初始化的常量 Tool::Version,

工具.rb:

class Tool < ActiveRecord::Base
  attr_accessible :name
  validates_presence_of :name
  has_many :versions
end

versions.rb

class Versions < ActiveRecord::Base
  belongs_to :tool
  attr_accessible :tool_version
end

abc.html.erb:

<%= form_for([@tool, @tool.versions.build]) do |f| %>
    <div class="field">
        <%= f.label :version %><br />
        <%= f.text_field :version %>
    </div>
    <div class="actions">
        <%= f.submit %>     
    </div>

<% end %>

在提交时我得到了错误。我在这里做错了什么??

【问题讨论】:

    标签: ruby-on-rails associations has-many belongs-to uninitialized-constant


    【解决方案1】:

    你的 text_field 应该是 tool_version 而不是 version

    【讨论】:

      【解决方案2】:

      你应该改变类名

      类名应该是Version,而不是Versions

      class Versions < ActiveRecord::Base
        belongs_to :tool
        attr_accessible :tool_version
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多