0

I am using ActiveAdmin in my rails application.My setup is the following:

app/models/translation_type.rb

class TranslationType < ActiveRecord::Base
    has_many :translation_details
    accepts_nested_attributes_for :translation_details, allow_destroy: true
end

app/models/translation_detail.rb

class TranslationDetail < ActiveRecord::Base
  belongs_to :translation_type
  attr_accessor :id, :language_from, :language_to, :price
end

app/admin/translation_type.rb

ActiveAdmin.register TranslationType do
  permit_params :translation_type, translation_details_attributes: [:id, :language_from, :language_to, :price, :_destroy]

  index do
    column :translation_type
    column :translation_details
    column :created_at
    column :updated_at
    actions
  end

  filter :translation_type
  filter :created_at

  form do |f|
    f.inputs "Translation Type Details" do
      f.input :translation_type
      f.input :created_at
      f.input :updated_at
    end
    f.inputs do
      f.has_many :translation_details, allow_destroy: true do |tran_det|
        tran_det.input :language_from, :collection => ['Gr', 'En', 'Fr', 'De']
        tran_det.input :language_to, :collection => ['Gr', 'En', 'Fr', 'De']
        tran_det.input :price
      end
    end
    f.actions
  end

  controller do
    before_filter { @page_title = :translation_type } 
  end

I don't need a section for translation_detail so I have omited app/admin/translation_detail.rb

I want to have nested forms of translation_detail in my translation_type form. Using the code above creates the nested form but after submit the attributes of translation_detail are not saved. Furthermore when I try to update or delete a translation_detail this message is shown

Couldn't find TranslationDetail with ID=* for TranslationType with ID=*

How can this be fixed?

igavriil
  • 1,001
  • 2
  • 12
  • 18

2 Answers2

0

It seem that this line

attr_accessor :id, :language_from, :language_to, :price

in app/models/translation_detail.rb, was causing the error, but I am not really sure why.

igavriil
  • 1,001
  • 2
  • 12
  • 18
0

I ran into the same problem when saving a field that previously wasn't being saved and did not exist in the DB. I had to remove the attr_accessor :fieldName from the model file to make it permanent.

As this answers states: attr_accessor on Rails is only ever used if you don't want the attributes to be saved to the database, only stored temporarily as part of an instance of that Model.

What probably was happening in the question's example is that it could not save the TranslationDetail because everything except for its relationship to TranslationType was being discarded before saving.

A. Rosas
  • 171
  • 2
  • 5