1

I have a problem with my following setup. It is a polymorphic n:m association between the feature, page and slide model. the join model is the slideshow model.

feature.rb - a model in a refinerycms extension

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 - a decorator for the refinery page model

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 - a model in a refinerycms extension

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 - a model in a refinerycms extension

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

I think the associations are correct, but I have a problem creating new slides within the feature#form.

feature-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 %> 

If I hit "save" I get following error:

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

My params hash looks as following:

{"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}

My controller looks as following:

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

I use Rails 4.1.5 and refinerycms. But I think it has nothing to do with refinerycms, just with normal Rails behavior.

I already took a look at following resources:

Anybody an idea what I am missing?

Community
  • 1
  • 1
Matthias
  • 4,355
  • 2
  • 25
  • 34

1 Answers1

0

try to create method "new" something like this

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

and edit method

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