Simple_form: Simple_form in Rails 6 can't render nested attributes

Created on 3 May 2020  路  4Comments  路  Source: heartcombo/simple_form

The title is quite self-explanatory.

Here are the models:

class Adventure < ApplicationRecord
  alias_attribute :pcs, :player_characters

  has_many :player_characters, dependent: :destroy
  accepts_nested_attributes_for :player_characters
end

class PlayerCharacter < ApplicationRecord
  belongs_to :adventure
end

And here my views:

# _form.html.erb

<%= simple_form_for @adventure do |f| %>
  <%= f.input :email %>
  <%= f.input :test %>
  <h3>PCs</h3>
    <%= f.simple_fields_for :player_characters do |player_character| %>
      <%= render 'player_character_fields', f: player_character %>
    <% end %>
  <%= f.submit %>
<% end %>

# _player_character_fields.html.erb

<div class='nested-fields'>
  <%= f.input :path %>
  <%= f.input :race %>
  <%= f.input :name %>
  <%= f.input :player_name %>
</div>

With this code the simple fields just don't render. If I remove accepts_nested_attributes_for from the model the simple fields render, but I can't create a new nested model as intended. If I change the code to the rails inbuilt form_for everything works as intended.

Ruby 2.6.3
Rails 6.0.2.2
Simple_form 5.0.2

needs debugging

Most helpful comment

I've the same issue, so it's still actual.
Gonna make a sample project this evening.

All 4 comments

@Naokimi Thanks for the report 馃檶

Could you please provide us a sample application that reproduces the issue in isolation?

That would help us find the issue.

huh,
I swear I had it happening in two different repositories but now I went back to them, rewrote the forms with simple_form and both seem to be working fine...
I'll close the issue for now.

I've the same issue, so it's still actual.
Gonna make a sample project this evening.

Update: https://github.com/Vinnie1991/simple-forms_nesting

The models are

class Event < ApplicationRecord
  has_many :time_records
  accepts_nested_attributes_for :time_records
end
class TimeRecord < ApplicationRecord
  belongs_to :event
end

Form is:

<%= simple_form_for(@event) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <%= f.input :title %>

    <%= f.simple_fields_for :time_records do |form| %>

      <%= form.date_field :from_date %>
      <%= form.date_field :till_date %>

    <% end %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

When I use accepted_nested_params, the two datefields disappear from the view.

Ruby 2.6.6
Rails 6.0.3.2
SimpleForms 5.0.2

Was this page helpful?
0 / 5 - 0 ratings