active_admin嵌套表单的未允许参数

时间:2015-10-09 09:03:18

标签: ruby-on-rails activeadmin

我在Active admin资源中使用嵌套表单,但嵌套属性的值未保存在模型中。

这是我的模型的详细信息 -

class customer_classes:
    # create the base class
    base = declarative_base(metadata=metadata)

    def __init__(self, metadata, customer_id):

        class analytics(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        class embeds(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        class downloads(self.base):
            __tablename__ = 'analytics_{}'.format(customer_id)
            __mapper_args__ = ...

        # now lets assign all these classes to attributes
        import inspect
        defined_classes = [cls for cls in locals().values()
                           if inspect.isclass(cls) and issubclass(cls, base)]
        self.__dict__.update({cls.__name__: cls for cls in defined_classes})

# now we can create a set of classes for customer 123
customer_model = customer_classes(123)
session.query(customer_model. analytics).all()

# you can just as easily work with another customer at the same time in the same session
another_model = customer_classes(456)
session.query(another_model.analytics).all()

下一个模型 -

class Exceed::Assessment < ActiveRecord::Base

    has_many :assessment_infos, :class_name => "Exceed::AssessmentInfo", :dependent => :destroy
    accepts_nested_attributes_for :assessment_infos, allow_destroy: true

end

这是Exceed :: Assessment

的活动管理资源
class Exceed::AssessmentInfo < ActiveRecord::Base

    belongs_to :assessment

end

从我的有效管理表单中填写beyond_assessment和嵌套表单assessment_info的详细信息。 Exceed_assessment详细信息已成功保存在模型中,但assessment_info未保存。 当我在控制台上检查它时显示错误信息 -

ActiveAdmin.register Exceed::Assessment do

  form do |f|
    f.inputs "Exceed Details" do
      f.input :exceed_id, :label => 'Exceed Type', :as => :select, :collection => Exceed.all.map{|s| ["#{s.exceed_type}", s.id]}
      f.input :exceed_section_id, :label => 'section', :as => :select, :collection => ExceedSection.all.map{|s| ["#{s.section}", s.id]}
      f.input :guideline, label: "Section Guideline"   
    end
    f.has_many :assessment_infos do |q|
      q.input :information
    end
    f.actions
  end

  controller do
    def permitted_params
      params.permit exceed_assessment: [:exceed_id, :exceed_section_id, :guideline],
      assessment_infos_attributes: [:information]
    end
  end


end

1 个答案:

答案 0 :(得分:0)

在纠正错误后,我犯了一些愚蠢的错误。

首先,在我的 Assessment_info 模型中,有一个列'beyond_assessment_id'。我将其更改为'assessment_id',因为我的模型是belongs_to评估模型。

其次我稍微改变了Permit params的语法,就像这样 -

controller do
    def permitted_params
      params.permit exceed_assessment: [:id, :exceed_id, :exceed_section_id, :guideline,
      assessment_infos_attributes: [:assessment_id, :information],]
    end
  end