【问题标题】:Rails 4 CollectionProxy errorRails 4 CollectionProxy 错误
【发布时间】:2017-02-27 05:30:44
【问题描述】:

我在一个名为 bill 的模型的表单上有一个集合,该模型用一个文本框显示该集合 #<Sponsorship::ActiveRecord_Associations_CollectionProxy:0x007f99a874bcd0> 而不是下拉菜单。有什么想法吗?

bill.rb

class Bill < ActiveRecord::Base
        belongs_to :congress_person
        has_many :bps, :dependent => :destroy
        has_many :committees, :dependent => :destroy
        has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship" 
        has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship"

        has_many :sponsors, class_name: 'CongressPerson', through: :sponsorships
        has_many :cosponsors, class_name: 'CongressPerson', through: :cosponsorships

congress_people.rb

has_many :sponsorships, -> { where(kind: :primary) }, class_name: "Sponsorship", foreign_key: :sponsor_id
    has_many :cosponsorships, -> { where(kind: :secondary) }, class_name: "Sponsorship", foreign_key: :sponsor_id

    has_many :sponsored_bills, through: :sponsorships, source: :bill
    has_many :cosponsored_bills, through: :cosponsorships, source: :bill

sponsorship.rb

belongs_to :bill
belongs_to :sponsor, class_name: "CongressPerson"
belongs_to :cosponsor, class_name: "CongressPerson"

_form.html.haml

...
.row-fluid
                .col-md-12
                    %h3 Sponsor
                    #sponsors-form
                        = f.simple_fields_for :sponsors do |sponsor|
                            = render 'sponsor_fields', f: sponsor
                        .links
                        = link_to_add_association 'Add Sponsor', f, :sponsors, class: 'btn btn-secondary add-button'
...

_sponsors_fields.html.haml

.form-inline.clearfix
    .row
        .nested-fields
            = f.input :sponsorships, :collection => @congress_people

            = link_to_remove_association "Remove", f, class: "form-control btn btn-secondary", style: 'height:20%; display:inline; float:right;' 

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 simple-form


    【解决方案1】:

    使用 simple_form,如果您接受多个:true,则可能会发生这种情况。 否则,这应该足够了:

    = f.input :sponsorships, :collection => @congress_people
    

    此外,@congress_people 需要在您的控制器方法中可用。

    【讨论】:

    • 我使用的是haml,它不使用开始和结束标签。
    • 我在我的 _sponsors_fields.html.haml 中使用了准确的输入,但我不使用 multiple:true。由于这是一个关联错误,我假设它与我的模型有关,但我无法弄清楚。
    • 你应该尝试找到@congress_people 并确保它得到正确的输入
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 2014-03-25
    相关资源
    最近更新 更多