Polymorphic has-many-through accepts_nested_attributes_for错误

时间:2014-08-20 12:17:28

标签: ruby-on-rails-4 polymorphic-associations refinerycms

我的以下设置存在问题。它是特征,页面和幻灯片模型之间的多态n:m关联。连接模型是幻灯片模型。

feature.rb - refinerycms扩展中的模型

module Refinery
  module MyExtension
    class Feature < Refinery::Core::BaseModel

      # associations
      has_many :slideshows, :as => :slideable, :class_name => "::Refinery::MyExtension::Slideshow"
      has_many :slides, :through => :slideshows, :class_name => "::Refinery::MyExtension::Slide"

      accepts_nested_attributes_for :slides, :allow_destroy => :false
    end
   end
end

page_decorator.rb - 炼油厂页面模型的装饰器

Refinery::Page.class_eval do

  # associations
  has_many :slideshows, :as => :slideable, :class_name => "Refinery::MyExtension::Slideshow"
  has_many :slides, :through => :slideshows, :class_name => "Refinery::MyExtension::Slideshow"

end

slide.rb - refinerycms扩展中的模型

module Refinery
  module MyExtension
    class Slide < Refinery::Core::BaseModel

      # associations
      has_many :slideshows, :class_name => "::Refinery::MyExtension::Slideshow"
      has_many :pages, :through => :slideshows, :source => :slideable, :source_type => "::Refinery::Page"
      has_many :features, :through => :slideshows, :source => :slideable, :source_type => "::Refinery::MyExtension::Feature"
    end
  end
end

slideshow.rb - refinerycms扩展中的模型

module Refinery
  module MyExtension
    class Slideshow < Refinery::Core::BaseModel

      # associations
      belongs_to :slide, :class_name => "::Refinery::MyExtension::Slide"
      belongs_to :slideable, :polymorphic => true

      accepts_nested_attributes_for :slide, :allow_destroy => :false
    end
  end
end

我认为关联是正确的,但我在功能#form中创建新幻灯片时遇到问题。

特征形式:

<%= form_for [refinery, :my_extension_admin, @feature] do |f| -%>
  <%= f.text_field :field1 %>
  <%= f.text_field :field2 %>
  <%= "some-more-fields..." %>

  <% slideshows = @feature.slideshows.empty? ? @feature.slideshows.build : @feature.slideshows %>
  <%= f.fields_for(:slideshows, slideshows) do |slideshow_form| %>
    <%= slideshow_form.hidden_field :position, :value => 0 %>
    <% slide = slideshow_form.object.slide.nil? ? slideshow_form.object.build_slide : slideshow_form.object.slide %>
    <%= slideshow_form.fields_for(:slide, slide) do |slide_form| %>
      <%= slide_form.text_field :a_field -%>
      <%= slide_form.text_field :another_field -%>
    <% end %>
  <% end %>
<% end %> 

如果我点击“保存”,我会收到以下错误:

::Refinery::MyExtension::Slideshow(#70206105711540) expected, got Array(#70206014033940)

我的params hash看起来如下:

{"utf8"=>"✓", "authenticity_token"=>"my-token", "switch_locale"=>"de", "feature"=>{"field1"=>"a value", "field2"=>"some value", "slideshows"=>{"position"=>"0", "slide_attributes"=>{"a_field"=>"some value", "another_field"=>"some value"}}}, "action"=>"create", "controller"=>"refinery/my_extension/admin/features", "locale"=>:de}

我的控制器如下所示:

module Refinery
  module MyExtension
    module Admin
      class FeaturesController < ::Refinery::AdminController

        crudify :'refinery/my_extension/feature', :title_attribute => 'name', :xhr_paging => true

        def feature_params
          params.require(:feature).permit(:field1, :field2, :slideshows => [:position, :slide => [:a_field, :anothter_field]])
        end

      end
    end
  end
end

我使用Rails 4.1.5和refinerycms。但我认为它与refinerycms无关,只是与普通的Rails行为有关。

我已经看过以下资源:

有人知道我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

尝试创建方法&#34; new&#34;像这样的东西

def new
  @feature = ::Refinery::MyExtension::Feature.new
  @feature.slideshows.build
end

和编辑方法

def edit
  @feature = ::Refinery::MyExtension::Feature.find(params[:id])
  @feature.slideshows.build
end