-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Nested Models
Nested Models let you build forms that update an object and its associated objects all in one shot.
To use nested models with simple_form, there are two rails features that you have to understand:
-
accepts_nested_attributes_for
- anActiveRecord
class method that goes in your model code - it lets you create and update child objects through the associated parent. An in depth explanation is available in theActiveRecord
documentation. -
fields_for
- a helper method that goes in your view code - it builds form fields for your nested models
Let's take a simple example:
class Machine < ActiveRecord::Base
has_many :parts , inverse_of: :machine
accepts_nested_attributes_for :parts
end
class Part < ActiveRecord::Base
# name:string
belongs_to :machine
end
With these models, we can use simple_form to update the machine and its associated parts in a single form:
<%= simple_form_for @machine do |m| %>
<%= m.simple_fields_for :parts do |p| %>
<%= p.input :name %>
<% end %>
<% end %>
For 'new' action, build the nested model from the controller:
class MachinesController < ApplicationController
def new
@machine = Machine.new
@machine.parts.build
end
end
e.g. if Machine has_one Color
, accepts_nested_attributes_for
must be called with :color
.
accepts_nested_attributes_for :color
And in your Controller#new action, you should make a new instance for the :color attribute:
class MachinesController < ApplicationController
def new
@machine = Machine.new
@machine.build_color
end
end
If you have non-ActiveRecord models, then refer to Use with ActiveModel compliant models page.
To fill the label of the value field with the key attribute and give it an ID so we can grab it with Capybara, we can access the object in question by calling nested_form.object
<%= simple_form_for @user do |m| %>
<%= m.simple_fields_for :custom_fields do |c| %>
<%= c.input :field_key, as: :hidden %>
<%= c.input :field_value, label: c.object.field_key, input_html: { id: c.object.field_key } %>
<% end %>
<% end %>
If you need to get this working with Strong Parameters
Add the nested many-to-many attributes in the form of [ATTRIBUTE IN PLURAL]_[ATTRIBUTES WORD]: [LIST OF SYMBOL ATTRIBUTES]. For the machines example would be: parts_attributes: [:id, :name]
class MachinesController < ApplicationController
def machine_params
params.require(:machine)
.permit(:name,
:owner,
parts_attributes: [:id, :name])
end
end
For one-to-one relationships, use the singular form: image_attributes
.
Do the normal thing in the view with simple_fields_for
:
<%= simple_form_for @machine do |f| %>
<%= f.simple_fields_for :parts, part do |pf| %>
<%= pf.input :name %>
<% end %>
<% end %>
This page was created by the OSS community and might be outdated or incomplete. Feel free to improve or update this content according to the latest versions of SimpleForm and Rails to help the next developer who visits this wiki after you.
Keep in mind to maintain the guides as simple as possible and to avoid additional dependencies that might be specific to your application or workflow (such as Haml, RSpec, Guard and similars).