- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个真让我失望! :(
我正在尝试为我的用户模型制作一个嵌套模型表单,其中有一个复选框列表,可以选中或取消选中多个商店以通过模型人员配备管理商店。
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
# User Form
- for store in Store.all
%p
= check_box_tag "user[store_ids][]", store.id, @user.stores.include?(store)
= store.nickname
{"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)
@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
最佳答案
好吧,我想通了这一点。我在这里回答,所以我希望有一天可以帮助某人,但问题是对 Has Many Through 和 Has And Belongs To Many Associations 之间区别的基本疏忽。
如果您想在 Has Many Through 中处理复选框或多选列表,则关联没有为您生成的 '_ids' 方法,您需要自己编写一个。
在此处查看 Paul Barry 的文章:
http://paulbarry.com/articles/2007/10/24/has_many-through-checkboxes
多态的东西可能很复杂,但如果你真的被困住了,也许可以退后一步,确保不是更基本的东西让你搞砸了——对我有用!
下面是从 Paul 的博客修改的代码,如果你正在处理一个多态有很多通过(基本上你只需要使用属性哈希并编写你的 polymorphic_type 属性),如何处理这个问题:
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
关于ruby-on-rails - Rails 有很多通过多态的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4200540/
我正在创建一个“杀死”人的命令。我希望机器人返回消息“哈!你以为!@Author 死了!”如果他们 ping 机器人。 (我如何让机器人查看它是否被 ping 过?)答案已更新并且现在可以正常工作。
我有一个在heroku 上运行的应用程序,例如my-app.herokuapp.com。但是,如果我输入 ping -c 10 my-app.herokuapp.com 在Mac终端中,它显示请求超时
我在 minikube 集群中有一个 k8s 服务/部署(default 命名空间中的名称 amq: D20181472:argo-k8s gms$ kubectl get svc --all-nam
我有 2 个 EC2 Ubuntu 实例。它们共享相同的 VPC、子网和安全组。实例的防火墙已关闭。但是私网IP还是无法互相ping通。如何让这些实例互相 ping 通? 最佳答案 在安全组中,为“回
我可以连接到我的 wifi(另一台笔记本电脑在此网络上正常),但是浏览器不会加载网页,并且我无法 ping 通 google.com 我注意到的一件奇怪的事情是,如果我查看/etc/resolv.co
我在 Azure 上使用 PUBSUB 时遇到问题。 Azure 防火墙将关闭闲置任意时间的连接。对于时间长度存在很多争议,但人们认为大约是 5 - 15 分钟。 我使用 Redis 作为消息队列。为
我很无聊,因为我的开发服务器已关闭,我正在运行命令提示符以无限期地 ping 服务器,以便我看到它们何时停止超时并知道我可以再次工作。与此同时,我想制作一个 Air 应用程序来为我做这件事,所以当它开
是否可以向 nat 后面的主机发送回显请求 后。所有的 echo-request 都不包含目标主机的端口,因此如果有多个主机使用相同的外部 ip 地址,nat 将如何将 echo-reques
我按照以下链接创建了 azure 实例 http://michaelwasham.com/2013/09/03/connecting-clouds-site-to-site-aws-azure/ 我可
friend 们,我认为这是一件奇怪的事情(至少对我来说)。因为我了解到互联网上的每个域名都有一个对应的IP地址。它存储在 DNS 上的某个位置。 现在,这就是我从命令行 ping google.co
我正在尝试使用分配给 kube-dns 服务的集群 IP 从 dnstools pod ping kube-dns 服务。 ping 请求超时。在同一个 dnstools pod 中,我尝试使用暴露的
我按照以下链接创建了 azure 实例 http://michaelwasham.com/2013/09/03/connecting-clouds-site-to-site-aws-azure/ 我可
我有一个虚拟网络 vmnet2,使用 10.0.2.0/24 网络,我希望我的 Linux 服务器能够 ping 默认网关。 我已将 Linux eth1 值设置为 IPADDR="10.0.2.50
我想将我的本地 mysql 数据库迁移到 Amazon RDS。但首先我想测试它是否正在接收通信。所以我尝试ping它。但是尝试超时。 ping -c 5 myfishdb.blackOut.us-w
我对 AWS 很陌生,已经测试过启动一个实例,如下所示: 下面是安全组,附加了inbound规则 我的问题是我无法 ping 通这台服务器。我可以知道我是否理解错了什么吗? 最佳答案 您需要为其创建新
我对 AWS 很陌生,已经测试过启动一个实例,如下所示: 下面是安全组,附加了inbound规则 我的问题是我无法 ping 通这台服务器。我可以知道我是否理解错了什么吗? 最佳答案 您需要为其创建新
如何确定 IP 地址是否可 ping 通?另外,如何使用 perl 脚本找到可 ping 的 IP 是静态的还是动态的? 最佳答案 看看 Net::Ping模块; #!/usr/bin/env per
我已经研究这个有一段时间了。对于网站 static.etreeblog.com,如果网站离线,我想更改 duv 的类。 我研究过的方法: - 使用带有图像的 onerror 标签来运行函数。-问题:我
我正在使用 OpenvSwitch-2.5.2 在两个虚拟机上设置第 2 层网络,如上图所示。 在阅读了 ovs 官方教程和其他一些文章后,我在每个虚拟机上尝试了以下命令: # on vm1 ip l
我有一个名为 backend 的 Docker 容器,它公开了一个端口 8200,并在其中的 gunicorn 后面运行了一个 django 服务器。这是我的 Dockerfile: FROM deb
我是一名优秀的程序员,十分优秀!