Rails,Cocoon Gem - NilClass:Class的未定义方法`reflect_on_association'

时间:2016-10-26 20:18:17

标签: ruby-on-rails ruby nested-forms cocoon-gem

我正在尝试在我的Rails 5应用程序中为嵌套表单设置cocoon gem。

我遇到了其他人发布的问题,但看起来适用于他们的解决方案并不适用于我。

我有地址,组织和设置的模型。协会是:

地址

belongs_to :addressable, :polymorphic => true, optional: true

组织

has_many :addresses, as: :addressable
        accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

设置

  has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

我的设置控制器有:

def setting_params
      params.require(:setting).permit( :newsletter,
        addresses_attributes: [:id, :description, :unit, :building, :street_number, :street, :city, :region, :zip, :country, :time_zone, :latitude, :longitude, :_destroy]
       )
    end

我的路线是:

resources :organisations do
    namespace :contacts do
    resources :addresses
    resources :phones 
    end
  end

resources :users, shallow: true do
    scope module: :users do
      resources :assign_roles
      resources :identities
      resources :org_requests
      resources :profiles
      resources :settings do 
        namespace :contacts do
          resources :addresses
          resources :phones 
        end
      end 
    end

我的地址表格有:

我的设置表单有:

<%= simple_form_for [@user, @setting] do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :newsletter, as: :radio_buttons, label: "Subscribe to our newsletter" %>

    <%= simple_fields_for :addresses do |f| %>
            <%= f.error_notification %>
                <%= render 'contacts/addresses/address_fields', f: f %>
            <%= link_to_add_association 'Manage address', f, :addresses, partial: 'contacts/addresses/address_fields' %>    
        <% end %>   
  </div>
<% end %>  

错误说:

undefined method `reflect_on_association' for NilClass:Class

此行在错误消息中突出显示:

                <%= link_to_add_association 'Manage address', f, :addresses, partial: 'contacts/addresses/address_fields' %>        

对于其他人来说,问题一直是使用单数而不是复数;或者使用@addresses代替:地址(虽然有些解决方案已经建议它应该是@而不是:。

我已经尝试了每种组合中的每一种。

任何人都可以看到我需要做些什么来设置它吗?

采取EDMUND的建议

我可以从简单的表单文档中看到嵌套属性应该在表单中以不同的方式处理。

https://github.com/plataformatec/simple_form/wiki/Nested-Models#example-for-strong-parameters

我尝试将表单更改为:

<%= simple_fields_for "addresses_attributes[]", address do |f| %>

这也不起作用。我无法渲染页面进行检查。

此上下文中的错误行指出组织控制器中的新操作存在问题。那有:

 def new
    @organisation = Organisation.new
    @organisation.addresses_build
  end

错误消息显示:

undefined method `addresses_build' for #<Organisation:0x007fcf6ff25f08>
Did you mean?  addresses

我现在想知道是否需要构建address_attributes而不是address。但是当我尝试时,我得到一个错误,上面写着:

undefined method `addresses_attributes_build' for #<Organisation:0x007fcf72c26d68>
Did you mean?  addresses_attributes=

2 个答案:

答案 0 :(得分:1)

此错误来自Active Record。这是Rails 4.2.7中此方法的文档。不应该是Rails 5的重大变化。

http://apidock.com/rails/v4.2.7/ActiveRecord/Reflection/ClassMethods/reflect_on_association

Cocoon正在使用它来识别多态模型的所有者类。您可以通过在控制台中执行以下操作来验证这一点。

Address.reflect_on_association(:addressable)

这将为您提供多态关联反射

Address.reflect_on_association(:setting)

返回nil

并且您的accepts_nested_attributes_for依赖于该方法来查找子/归类。请参阅此处的源代码。

http://apidock.com/rails/v4.2.1/ActiveRecord/NestedAttributes/ClassMethods/accepts_nested_attributes_for

您的表单将地址嵌套在设置下。所以它无法找到父/所有者类。

答案 1 :(得分:0)

你必须写

f.simple_fields_for :addresses 

f引用表单,您需要它才能遍历所有子地址。

其次,最好让link_to_add_association在循环之外(否则你只能添加一个新地址?)

所以写点像

<%= f.simple_fields_for :addresses do |f| %>
  <%= render 'contacts/addresses/address_fields', f: f %>
<% end %>  
<%= link_to_add_association 'Manage address', f, :addresses, partial: 'contacts/addresses/address_fields' %>    

(imho你在简单形式的文档中提到的例子已经过时,不需要复杂的表格)