rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue

18,796

Solution 1

I found a better solution for you. You can use :for option in inputs helper.

f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
  meta_form.input :title
  meta_form.input :description
  meta_form.input :keywords
end

I think this might work too, but I didn't check

f.inputs :title, :desctiption, :keywords, 
  name: "Meta Data",
  for: [:meta_data, f.object.meta_data || MetaData.new]

Solution 2

In rails 4, this is something that works, with a nice design

e.g., A customer has one account

model/customer.rb

accepts_nested_attributes_for :account

admin/customer.rb

form do |f|
  f.inputs do
    f.input :user, input_html: { disabled: true }
      f.input :name
      f.input :address
      f.input :city
      f.input :country, as: :string
    end
    f.buttons

    f.inputs "Account Information", for: [:account, f.object.account] do |s|
      s.input :active, as: :boolean
      s.input :subscription, as: :boolean
      s.input :expires_on, as: :datepicker

      s.actions
    end
  end

  controller do
    def permitted_params
      params.permit!
    end
  end
end

Solution 3

i was having the same problem, i worked in your hack and got it working. i then moved <% @page.build_meta_data %> to a custom new method like this

  controller do
    def new
      @tenant = Tenant.new
      @tenant.build_tenant_configurable
    end
  end

hope this helps

Share:
18,796

Related videos on Youtube

thrice801
Author by

thrice801

Updated on April 19, 2020

Comments

  • thrice801
    thrice801 about 4 years

    I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. -- (page has_one meta_data, accepts_nested_attributes_for meta_data)

    Example 1) in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...

    form do |f|
      f.inputs "Page Information" do
        f.input :name
        f.input :uri
        f.input :view
        f.input :body, :as => :text
        f.input :active
      end
    
      f.inputs "Meta Data" do
        f.semantic_fields_for :meta_data do |meta_form|
          meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
        end
      end  
    end
    

    I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this

    2) How I am working around it, but seems hacky

    <%= semantic_form_for [:admin, @page] do |f| %>
      <% @page.build_meta_data %>
      <%= f.inputs :name => "Page Information" do  %>
        <%= f.input :name %>
        <%= f.input :uri %>
        <%= f.input :view %>
        <%= f.input :body, :as => :text %>
        <%= f.input :active %>
      <% end %>
      <%= f.semantic_fields_for :meta_data do |meta_form| %>
        <%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
      <% end %>
    
      <%= f.buttons %>
    <% end %>
    

    Thanks in advance for any help or clarification.

    (note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)

  • thrice801
    thrice801 over 12 years
    thanks, exactly what I was looking for and couldn't find anywhere!
  • Trip
    Trip almost 11 years
    I wish this worked for me. The form builds but inside of the <li>, there is no html for a hidden nested value. Formtastic 2.2.1
  • Trip
    Trip almost 11 years
    Here's what I did : = f.inputs "Registered Course", :for => [:registered_courses, f.object.new_record? ? RegisteredCourse.new : f.object.registered_courses] do |registered_course_form| = registered_course_form.input :enterprise_registration_id, input_html: { value: registration.id }, as: :hidden I needed to use new_record? because its a HABTM, therefore an empty array is still a value.
  • Hans de Wit
    Hans de Wit about 10 years
    This works great, but instead of permitting all attributes by overriding controller method, you could do something like this: permit_params :user, :name, account_attributes: [:active, :subscription, ...]. This worked for me!
  • akira
    akira over 9 years
    Don't forget to also permit the :id parameter, or else you won't be able to update (you will create a new record instead). See Setting up Strong Parameters