Skip to content

Instantly share code, notes, and snippets.

@desnudopenguino
Last active May 26, 2017 04:07
Show Gist options
  • Save desnudopenguino/d886f2851805fc392532706c80ba30e7 to your computer and use it in GitHub Desktop.
Save desnudopenguino/d886f2851805fc392532706c80ba30e7 to your computer and use it in GitHub Desktop.
Contact and Address saving with Rails 5
<% prefix = "contact_address" %>
<%= fields_for prefix, @address do |address_form| -%>
<div class="field">
<%= address_form.label :name %>
<%= address_form.text_field :name, id: :address_name %>
</div>
<div class="field">
<%= address_form.label :street %>
<%= address_form.text_field :street, id: :address_street %>
</div>
<div class="field">
<%= address_form.label :city %>
<%= address_form.text_field :city, id: :address_city %>
</div>
<div class="field">
<%= address_form.label :state %>
<%= address_form.text_field :state, id: :address_state %>
</div>
<div class="field">
<%= address_form.label :zip %>
<%= address_form.text_field :zip, id: :address_zip %>
</div>
<div class="field">
<%= address_form.label :country %>
<%= address_form.text_field :country, id: :address_country %>
</div>
<div class="field">
<%= address_form.label :primary %>
<%= address_form.check_box :primary, id: :address_primary %>
</div>
<% end %>
<%= form_with(model: contact, local: true) do |form| %>
<% if contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(contact.errors.count, "error") %> prohibited this contact from being saved :</h2> <ul> <% contact.errors.full_messages.each do |message| %> <li><%= message %></li> <% end %> </ul>
</div>
<% end %>
<%= form.hidden_field :list_id, id: :contact_list_id, value: @list.id %>
<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :contact_name %>
</div>
<div class="field">
<%= form.label :active %>
<%= form.check_box :active, id: :contact_active %>
</div>
<%= render "contacts/address" %>
<div class="field">
<%= form.label :comment %>
<%= form.text_area :comment, id: :contact_comment %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
def create
@contact = Contact.new(contact_params)
respond_to do |format|
if @contact.save
@contact_address = @contact.addresses.build(contact_address_params)
@contact_address.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
...
# Never trust parameters from the scary internet, only allow the white list through.
def contact_params
params.require(:contact).permit(:list_id, :name, :active, :comment)
end
def contact_address_params
params.permit(:contact_address).permit(:name, :street, :city, :state, :zip, :country, :primary)
end
class Contact < ApplicationRecord
belongs_to :list
has_many :addresses
has_many :contact_meta
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment