Skip to content

Instantly share code, notes, and snippets.

@dennym
Last active December 27, 2015 06:29
Show Gist options
  • Save dennym/7281586 to your computer and use it in GitHub Desktop.
Save dennym/7281586 to your computer and use it in GitHub Desktop.
<%= simple_form_for(@invoice) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :address_sender %>
<%= f.input :address_recipient %>
<%= f.simple_fields_for :invoice_line_items do |invoice_line_items_form| %>
<div class="duplicatable_nested_form">
<%= invoice_line_items_form.input :description %>
<%= invoice_line_items_form.input :price %>
<%= invoice_line_items_form.input :amount %>
<% if invoice_line_items_form.object.new_record? %>
<%= link_to "Remove", "", :remote => true, :class => "destroy_duplicate_nested_form" %>
<% else %>
<%= link_to "remove", invoice_invoice_line_item_path(@invoice, invoice_line_items_form.object), :method => :delete, :remote => true, :class => "destroy_duplicate_nested_form" %>
<%= invoice_line_items_form.input :id, as: :hidden %>
<% end %>
</div>
<% end %>
</div>
<div class="form-actions">
<%= link_to "Add Another Item", "", :class => "duplicate_nested_form" %>
<%= f.button :submit %>
</div>
<% end %>
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
jQuery ($) ->
$(document).ready ->
if $('.duplicatable_nested_form').length
nestedForm = $('.duplicatable_nested_form').last().clone()
$(".destroy_duplicate_nested_form:first").remove()
$('.destroy_duplicate_nested_form').live 'click', (e) ->
$(this).closest('.duplicatable_nested_form').slideUp().remove()
$('.duplicate_nested_form').click (e) ->
e.preventDefault()
lastNestedForm = $('.duplicatable_nested_form').last()
newNestedForm = $(nestedForm).clone()
formsOnPage = $('.duplicatable_nested_form').length
$(newNestedForm).find('label').each ->
oldLabel = $(this).attr 'for'
newLabel = oldLabel.replace(new RegExp(/_[0-9]+_/), "_#{formsOnPage}_")
$(this).attr 'for', newLabel
$(newNestedForm).find('select, input').each ->
oldId = $(this).attr 'id'
newId = oldId.replace(new RegExp(/_[0-9]+_/), "_#{formsOnPage}_")
$(this).attr 'id', newId
oldName = $(this).attr 'name'
newName = oldName.replace(new RegExp(/\[[0-9]+\]/), "[#{formsOnPage}]")
$(this).attr 'name', newName
$( newNestedForm ).insertAfter( lastNestedForm )
class InvoicesController < ApplicationController
before_filter :authenticate_user!
before_action :set_invoice, only: [:show, :edit, :update, :destroy]
# GET /invoices
# GET /invoices.json
def index
@invoices = current_user.invoices
end
# GET /invoices/1
# GET /invoices/1.json
def show
end
# GET /invoices/new
def new
@invoice = Invoice.new
@invoice.invoice_line_items.build
end
# GET /invoices/1/edit
def edit
end
# POST /invoices
# POST /invoices.json
def create
@invoice = current_user.invoices.new(invoice_params)
@invoice.status = "Pending"
respond_to do |format|
if @invoice.save
format.html { redirect_to @invoice, notice: 'Invoice was successfully created.' }
format.json { render action: 'show', status: :created, location: @invoice }
else
format.html { render action: 'new' }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /invoices/1
# PATCH/PUT /invoices/1.json
def update
respond_to do |format|
if @invoice.update(invoice_params)
format.html { redirect_to invoices_path, notice: 'Invoice was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @invoice.errors, status: :unprocessable_entity }
end
end
end
# DELETE /invoices/1
# DELETE /invoices/1.json
def destroy
@invoice.destroy
respond_to do |format|
format.html { redirect_to invoices_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions and check if the user has access to it.
def set_invoice
@invoice = current_user.invoices.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def invoice_params
params.require(:invoice).permit(:address_sender, :address_recipient, :status, :user_id, :customer_id, invoice_line_items_attributes: [:id, :description, :price, :amount])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment