문제

나는 매우 기본적인 협회가 있습니다.

# 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

그러나 양식을 저장하면 모든 관련 구독이 파괴됩니다. 확인란을 확인하면 삭제되지 않지만 그러나 확인란도 저장되지 않습니다. 내가 뭘 잘못하고 있는지 아는 사람 있나요?

도움이 되었습니까?

해결책

거의 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] 비어 있었고 전체 연관성을 삭제했습니다.

다른 팁

양식으로 구독을 제출하지 않습니다. 확인란을 클릭하지 않으면 해당 구독을 제출할 것이 없으므로 중첩 된 속성 기능에 의해 구독이 사라지고 있습니다. 구독의 서비스 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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top