Skip to content

Instantly share code, notes, and snippets.

@andyjbas
Created September 15, 2012 00:53
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 andyjbas/3725875 to your computer and use it in GitHub Desktop.
Save andyjbas/3725875 to your computer and use it in GitHub Desktop.
# orders show
<%= @order.status %>
Line_items:
<br>
<% @order.line_items.each do |item| %>
<%= item.notes %>, <%= item.units %>
<% end %>
# orders create action
def create
@order = Order.new(params[:order])
LineItem.create(params[:order][:line_items_attributes][0])
respond_to do |format|
if @order.save
format.html { redirect_to @order, notice: 'Order was successfully created.' }
format.json { render json: @order, status: :created, location: @order }
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
end
end
end
# order form partial
<%= form_for(@order) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.text_field :status %>
<%= f.fields_for :line_items do |l| %>
<%= l.label :notes %>
<%= l.text_field :notes %>
<%= l.label :units %>
<%= l.text_field :units %>
<% end %>
<%= f.submit %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment