2009-12-03 3 views
2

I 꽤 기본적인 관계를 fields_for :has_many 가진 문제점 : 통해

# 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 

구독도 부울 열 I 개별적으로 구성 할 필요가 "통지"를 가지고, 그래서이 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 

그러나 양식을 저장하면 관련된 모든 구독이 삭제됩니다. 반면 확인란을 선택하면 삭제되지 않을 것이지만 체크 박스는으로 저장되지 않습니다. 아무도 내가 뭘 잘못하고 있는지 알아?

답변

2

거의 2 시간 동안 시도한 후에, 마침내 작동하게되었습니다. 코드에 약간의 변화가 충분 했 : 비어

# _form.html.haml 
# […] 
- if current_user.subscriptions.length > 0 
    %fieldset#subscriptions 
    %legend Abonnements 
    %table 
     %tr 
     %th.name 
     %th.notification Notifications? 
     - f.fields_for :subscriptions do |sub| 
     %tr 
      %td= sub.object.service.name 
      %td 
      = sub.check_box :notification 
      = hidden_field_tag "user[service_ids][]", sub.object.service.id 
# […] 

params[:user][:service_ids] 때문에, 전체 연결을 삭제.

0

양식에 가입을 제출하고 있지 않습니다. 확인란을 클릭하지 않으면 해당 구독을 제출할 것이 아무것도 없기 때문에 중첩 된 특성 기능에 의해 구독이 지워집니다. 구독 서비스 ID가있는 숨겨진 필드를 넣으십시오.

중첩 된 속성에 대한 양식을 잘못 설정했다고 생각합니다. 이것을 시도하십시오 :

- if current_user.subscriptions.length > 0 
    %fieldset#subscriptions 
    %legend Abonnements 
    %table 
     %tr 
     %th.name 
     %th.notification Notifications? 
     - f.fields_for :subscriptions do |sub| 
     %tr 
      %td= sub.object.service.name 
      %td 
      = sub.check_box :notification 
      = sub.hidden_field :service_id 
+0

아무 것도 바뀌지 않습니다. 좋은 점은 있지만, 'for'는 전혀 필요하지 않습니다. –