基本上我的问题是,当尝试在我的 Rails 应用程序中使用嵌套视图时,我需要同时存储页面的一般用户和管理员。两者都包含相同的信息,我只需要知道谁是谁。我有一个团队模型:
class Team < ActiveRecord::Base
attr_accessible :auth_token, :team_name, :template
has_many :events, :dependent => :destroy
has_many :users
has_many :admins, :class_name => "User", :foreign_key => 'admin_id'
accepts_nested_attributes_for :admins
end
我的 Teams 表架构包含一个 admin_id 字段。我不明白为什么会收到此错误。我已经尝试了相当多的谷歌搜索和论坛搜索,尽管发现了一些类似的问题,但我无法找到我遇到的所有相同问题组合。
提前致谢。
编辑 用户模型:
class User < ActiveRecord::Base
require 'digest/sha1'
attr_accessor :pre_pass
belongs_to :team
attr_accessible :name, :player
attr_protected :password
validates :name, :presence => true
validates :pre_pass, :presence => true
before_save :encrypt_pass
after_save :clear_pre_pass
end
团队/新视图
<%= form_for(@team) do |f| %>
<% if @team.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@team.errors.count, "error") %> prohibited this team from being saved:</h2>
<ul>
<% @team.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :team_name %><br />
<%= f.text_field :team_name %>
</div>
<div class="field">
<%= f.label :template %><br />
<%= f.number_field :template %>
</div>
<div class="field">
<%= f.label :auth_token %><br />
<%= f.number_field :auth_token %>
</div>
<p>
<%= f.fields_for :admins do |builder| %>
<%= render "admin_fields", :f => builder %>
<% end %>
</p>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
TeamsController#new 中的错误 ActiveRecord::UnknownAttributeError
unknown attribute: user_id
app/controllers/teams_controller.rb:28:in `new'
团队控制器#new
def new
@team = Team.new
@team.admins.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @team }
end
end