Created
September 8, 2014 17:40
-
-
Save gilbert/e89d1e716bfefb103141 to your computer and use it in GitHub Desktop.
DoubleDog Many-to-Many From
This file contains 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
<h2>Create New Order</h2> | |
<% if @error %> | |
<p>Error: <%= @error %> </p> | |
<% end %> | |
<%= form_tag orders_path, :method => :post do %> | |
<!-- Our goal is to an array of item ids --> | |
<select class="item-select" name="order[item_ids][]"> | |
<% @items.each do |item| %> | |
<option value="<%= item.id %>"><%= item.name %></option> | |
<% end %> | |
</select> | |
<br /> | |
<button class="add-item">Add Item</button> | |
<%= submit_tag "Create Order" %> | |
<% end %> | |
<script type="text/javascript"> | |
$('.add-item').on('click', function (e) { | |
e.preventDefault(); | |
var $itemSelect = $('.item-select:last'); | |
console.log("Item select", $itemSelect); | |
$itemSelect.after( $itemSelect.clone() ); | |
}); | |
</script> | |
<% @orders.each do |order| %> | |
<p> | |
Order #<%= order.id %> | |
<ul> | |
<% order.items.each do |item| %> | |
<li><%= item.name %></li> | |
<% end %> | |
</ul> | |
</p> | |
<% end %> |
This file contains 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 OrdersController < ApplicationController | |
def index | |
@orders = DoubleDog.db.all_orders | |
@items = DoubleDog.db.all_items | |
end | |
def create | |
tx_params = { session_id: $session_id } | |
tx_params[:items] = order_params[:item_ids].map! do |id| | |
DoubleDog.db.get_item(id.to_i) | |
end | |
result = DoubleDog::CreateOrder.new.run(tx_params) | |
if result[:success?] | |
redirect_to orders_path | |
else | |
@error = result[:error] | |
render 'index' | |
end | |
end | |
private | |
def order_params | |
params.require(:order).permit(item_ids: []) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment