我有一个非常基本的关联关系:问题用的has_many:通过和fields_for
# user.rb
class User < ActiveRecord::Base
has_many :services, :through => :subscriptions
has_many :subscriptions, :accessible => true
accepts_nested_attributes_for :subscriptions
end
# service.rb
class Service < ActiveRecord::Base
has_many :users, :through => :subscriptions
has_many :subscriptions
end
# subscription.rb
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :service
end
认购也有一个布尔列“通知”,我需要单独配置,所以我看着API,跟着例如并且想出了这个代码为我的形式:
- if current_user.subscriptions.length > 0
%fieldset#subscriptions
%legend Abonnements
%table
%tr
%th.name
%th.notification Notifications?
- for subscription in current_user.subscriptions do
%tr
- f.fields_for :subscriptions, subscription do |s|
%td=subscription.service.name
%td= s.check_box :notification
但是当我保存表单,所有相关预订被破坏。而当我检查复选框时,它不会被删除,,但复选框不保存。有谁知道我做错了什么?
这并没有改变任何东西 - 虽然很高兴知道,我根本不需要“for”。 – 2009-12-03 14:16:25