Ruby on rails Rails有许多通过多态的复选框
这个真让我失望( 我正在尝试为我的用户模型创建一个嵌套模型表单,表单中有一个复选框列表,在该列表中可以选中或取消选中多个存储,以通过模型配置管理存储Ruby on rails Rails有许多通过多态的复选框,ruby-on-rails,forms,checkbox,has-many-through,has-many-polymorphs,Ruby On Rails,Forms,Checkbox,Has Many Through,Has Many Polymorphs,这个真让我失望( 我正在尝试为我的用户模型创建一个嵌套模型表单,表单中有一个复选框列表,在该列表中可以选中或取消选中多个存储,以通过模型配置管理存储 class Staffing < ActiveRecord::Base # Associations belongs_to :user belongs_to :staffable, :polymorphic => true belongs_to :store, :class_name => "Store",
class Staffing < ActiveRecord::Base
# Associations
belongs_to :user
belongs_to :staffable, :polymorphic => true
belongs_to :store, :class_name => "Store",
:foreign_key => "staffable_id"
end
class User < ActiveRecord::Base
# Includes
acts_as_authentic
# Associations
has_many :staffings, :dependent => :destroy
has_many :stores, :through => :staffings
# Nested Attributes
accepts_nested_attributes_for :staffings, :allow_destroy => true
end
class Store < ActiveRecord::Base
# Associations
has_many :staffings, :as => :staffable, :dependent => :destroy
has_many :users, :through => :staffings
end
# Users Controller
def create
@user = User.new(params[:user])
flash.now[:notice] = "#{@user.name} was successfully created." if @user.save
respond_with @user
end
def update
@user = User.find(params[:id])
params[:user][:store_ids] ||= []
@user.update_attributes(params[:user])
flash.now[:notice] = "#{@user.name} was successfully updated."
respond_with @user
end
发送我的参数,如下所示:
{"utf8"=>"✓",
"authenticity_token"=>"blub-blub-blub=",
"user"=>{"login"=>"hey.buddy",
"email"=>"test@hey.net",
"role"=>"admin",
"hq"=>"1",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]",
"store_ids"=>["3",
"4"]}}
然后是错误
Mysql2::Error: Column 'staffable_type' cannot be null: INSERT INTO `staffings` (`created_at`, `staffable_id`, `staffable_type`, `updated_at`, `user_id`) VALUES ('2010-11-17 00:30:24', 3, NULL, '2010-11-17 00:30:24', 102)
我知道我必须把staffable_类型打造成“商店”,但我已经尝试了一整天——有什么诀窍
我确实将staffable_type(和id)列设置为:null=>false,但这不可能是导致此问题的原因,因为在命中DB之前,需要在控制器中解决此问题
为什么以下内容在我的创建操作中不起作用:
@user.staffings.each do |s|
s.staffable_type = 'Store'
end
或:
如果你尝试过许多类似于上述的事情,但都没有结果,任何帮助都将不胜感激
谢谢你的时间!好的,我想好了这个问题。我在这里回答这个问题是希望有一天我能帮助别人,但问题是基本上忽略了Has-Many和Has-Many和Has-Many之间的区别 如果您想在Has Many to中处理复选框或多选列表,则关联不会为您生成“\u id”方法,您需要自己编写一个 请参见Paul Barry的文章: 多态性的东西可能很复杂,但如果你真的很好,也许可以退一步,确保它不是更基本的东西把你搞砸了——对我有用 下面是Paul的博客中修改的代码,该代码介绍了如果您处理的是一个多态类型,那么如何处理这个问题(基本上,您只需要使用属性散列并编写多态类型属性):
@user.staffings.each do |s|
s.staffable_type = 'Store'
end
@user.store_ids.each do |i|
s = @user.staffings.build
s.staffable_type = 'Store'
s.staffable_id = i
end
attr_accessor :store_ids
after_save :update_stores
#after_save callback to handle group_ids
def update_stores
unless store_ids.nil?
self.staffings.each do |staffing|
staffing.destroy unless store_ids.include?(staffing.staffable_id.to_s)
store_ids.delete(staffing.staffable_id.to_s)
end
store_ids.each do |s|
self.staffings.create(attributes = {:staffable_id => s, :staffable_type => 'Store'}) unless s.blank?
end
reload
self.store_ids = nil
end
end