Skip to content

Instantly share code, notes, and snippets.

@universal
Forked from anonymous/form.html.erb
Last active October 17, 2016 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save universal/87d3e3766e898f10d90f87e3d4bc5533 to your computer and use it in GitHub Desktop.
Save universal/87d3e3766e898f10d90f87e3d4bc5533 to your computer and use it in GitHub Desktop.
<%= form_for @serialized_products_form, url: serialized_products_path do |f| %>
<%= f.fields_for :serialized_products, @serialized_products_form.serialized_products do |p| %>
<%= p.label :product_id %><br>
<%= p.text_field :product_id %>
<%= p.label :location_id %><br>
<%= p.text_field :location_id %>
<%= p.label :serial %><br>
<%= p.text_field :serial %>
<% end %>
<%= submit_tag %>
<% end %>
class SerializedProductsController < ApplicationController
before_action :set_serialized_product, only: [:show, :edit, :update, :destroy]
def new
if session[:reference] == nil or session[:location_id] == nil or session[:line_item_ids] == nil
print "\n\nSession is nil.\n\n"
redirect_back(fallback_location: root_path)
end
@reference = session[:reference]
location_id = session[:location_id]
@line_items = InventoryAdjustmentItem.where(id: session[:line_item_ids])
@serialized_products = []
@line_items.each do |line_item|
(line_item.quantity).times do
@serialized_products.push(SerializedProduct.new(product_id: line_item.product_id, location_id: location_id))
end
end
end
def create
params["serialized_products"].each do |serialized_product|
if serialized_product["serial"].present?
SerializedProduct.create(serialized_product_params(serialized_product))
end
end
redirect_to :root, notice: 'Serialized product was successfully created.'
session[:reference] = nil
session[:location_id] = nil
session[:line_item_ids] = nil
end
private
def set_serialized_product
@serialized_product = SerializedProduct.find(params[:id])
end
def serialized_product_params(my_params)
my_params.permit(:product_id, :location_id, :serial)
end
end
class SerializedProductsForm
attr_reader :serialized_products
def initialize(serialized_products:)
@serialized_products = serialized_products
end
def serialized_products_attributes=
...
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment