Created
November 4, 2022 17:27
-
-
Save nathanclark/aee73961006dc5a5eae4fa612baacb47 to your computer and use it in GitHub Desktop.
Nested Attributes Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% cancel_path ||= company.persisted? ? [:account, company] : [:account, @team, :companies] %> | |
<%= form_with model: company, url: (company.persisted? ? [:account, company] : [:account, @team, :companies]), local: true, class: 'form' do |form| %> | |
<%= render "shared/limits/form", form: form, cancel_path: cancel_path do %> | |
<%= render 'account/shared/forms/errors', form: form %> | |
<% with_field_settings form: form do %> | |
<%= render 'shared/fields/text_field', method: :name, options: {autofocus: true} %> | |
<%= render 'shared/fields/text_field', method: :legal_name %> | |
<%= render 'shared/fields/text_field', method: :ein %> | |
<%# ๐ super scaffolding will insert new fields above this line. %> | |
<% end %> | |
<h4>Addresses</h4> | |
<%= form.fields_for :postal_addresses do |address_form| %> | |
<div class="form-group"> | |
<%= render 'shared/fields/text_field', form: address_form, method: :street1 %> | |
<%= render 'shared/fields/text_field', form: address_form, method: :street2 %> | |
<%= render 'shared/fields/text_field', form: address_form, method: :city %> | |
<%= render 'shared/fields/text_field', form: address_form, method: :state %> | |
<%= render 'shared/fields/text_field', form: address_form, method: :zipcode %> | |
</div> | |
<% end %> | |
<div class="buttons"> | |
<%= form.submit (form.object.persisted? ? t('.buttons.update') : t('.buttons.create')), class: "button" %> | |
<%= link_to t('global.buttons.cancel'), cancel_path, class: "button-secondary" %> | |
</div> | |
<% end %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account::CompaniesController < Account::ApplicationController | |
account_load_and_authorize_resource :company, through: :team, through_association: :companies | |
# GET /account/teams/:team_id/companies | |
# GET /account/teams/:team_id/companies.json | |
def index | |
delegate_json_to_api | |
end | |
# GET /account/companies/:id | |
# GET /account/companies/:id.json | |
def show | |
delegate_json_to_api | |
end | |
# GET /account/teams/:team_id/companies/new | |
def new | |
@company.postal_addresses.new | |
end | |
# GET /account/companies/:id/edit | |
def edit | |
end | |
# POST /account/teams/:team_id/companies | |
# POST /account/teams/:team_id/companies.json | |
def create | |
respond_to do |format| | |
if @company.save | |
format.html { redirect_to [:account, @team, :companies], notice: I18n.t("companies.notifications.created") } | |
format.json { render :show, status: :created, location: [:account, @company] } | |
else | |
format.html { render :new, status: :unprocessable_entity } | |
format.json { render json: @company.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /account/companies/:id | |
# PATCH/PUT /account/companies/:id.json | |
def update | |
respond_to do |format| | |
if @company.update(company_params) | |
format.html { redirect_to [:account, @company], notice: I18n.t("companies.notifications.updated") } | |
format.json { render :show, status: :ok, location: [:account, @company] } | |
else | |
format.html { render :edit, status: :unprocessable_entity } | |
format.json { render json: @company.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /account/companies/:id | |
# DELETE /account/companies/:id.json | |
def destroy | |
@company.destroy | |
respond_to do |format| | |
format.html { redirect_to [:account, @team, :companies], notice: I18n.t("companies.notifications.destroyed") } | |
format.json { head :no_content } | |
end | |
end | |
private | |
include strong_parameters_from_api | |
def process_params(strong_params) | |
# ๐ super scaffolding will insert processing for new fields above this line. | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Company < ApplicationRecord | |
belongs_to :team | |
has_many :postal_addresses, dependent: :destroy, enable_updates: true, inverse_of: :company | |
accepts_nested_attributes_for :postal_addresses, allow_destroy: true, reject_if: :all_blank | |
validates :name, presence: true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment