我有一个关于我有一个协会的创建语句的问题。我对更新没有任何问题,但是当我使用简单的窗体关联创建新对象时,我在通过模型上的验证将所有事情都搞定了。嵌套有很多通过简单的表单多选择验证
形式
%div.inner-padding
= simple_form_for(@group, :wrapper => :bootstrap3_horizontal, :html => { multipart: true, :class => 'form-horizontal' }) do |f|
%section#group-show
%div.panel
%div
= f.input :name
= f.association :group_type, collection: current_user.organization.group_types.all, required: true unless current_user.organization.nil?
= create_link(:group_type, 'Create a new group type')
-if can? :access, @group, :organization
= f.association :organization, required: true
%div.group-users
%div.group-users-header
%h3 Users
%hr
%div
= f.association :users, collection: current_user.organization.users.all, as: :check_boxes, label_method: :basic_info,
include_blank: false, label: false, class:'list-group-item' unless current_user.organization.nil?
模型
class Group < ActiveRecord::Base
belongs_to :group_type
belongs_to :organization
has_many :user_groups, inverse_of: :group
has_many :users, through: :user_groups,
:after_remove => :alter_user,
:after_add => :alter_user,
inverse_of: :groups
accepts_nested_attributes_for :user_groups, allow_destroy: true
...
class UserGroup < ActiveRecord::Base
before_save :process_expirations unless :skip_callbacks
belongs_to :user
belongs_to :group, -> { includes :training_types }
accepts_nested_attributes_for :user
# # # # # # # # # # # # # # #
# Validations
# # # # # # # # # # # # # # #
validates :user_id,
:group_id,
:presence => true
validates_uniqueness_of :user_id, :scope => :group_id
class User < ActiveRecord::Base
rolify
devise :invitable, :database_authenticatable,
:recoverable, :rememberable, :trackable
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40},
:on => :create
validates :password, :confirmation => true,
:length => {:within => 6..40},
:allow_blank => true,
:on => :update
# # # # # # # # # # # # # # #
# Relations
# # # # # # # # # # # # # # #
has_many :user_groups
has_many :groups, through: :user_groups,
:after_remove => :alter_group,
:after_add => :alter_group,
:dependent => :destroy
has_many :training_histories, -> { includes :lecture }
has_many :training_types, through: :expirations,
:after_remove => :alter_training_type,
:after_add => :alter_training_type
控制器动作
def new
@group = Group.new
@group.user_groups.build.build_user
add_breadcrumb 'new group', new_group_path
end
我得到的错误是“user_groups.group_id”=> [“不能为空”],我敢肯定它是由我的user_groups模型中的验证引起的。
有什么建议吗?我试着按照这里的指南:http://robots.thoughtbot.com/accepts-nested-attributes-for-with-has-many-through但他们并没有真正做出多选择他们的选择。不知道什么方向来到这里,这是奇怪的,我不需要改变任何东西,它的作品更新完美...