Skip to content

Instantly share code, notes, and snippets.

@sammarcus
Created June 2, 2015 08:25
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 sammarcus/3b04b92030ea3eb172ac to your computer and use it in GitHub Desktop.
Save sammarcus/3b04b92030ea3eb172ac to your computer and use it in GitHub Desktop.
class Customer < ActiveRecord::Base
belongs_to :order
validates :name, presence: true,
allow_blank: false,
uniqueness: true
end
<h1>Place a new Ice Cream order</h1>
<p>
</p>
<%= link_to 'Orders', controller: 'orders' %>
<%= link_to 'Flavors', controller: 'flavors' %>
<%= link_to 'Toppings', controller: 'toppings' %>
<%= link_to 'Containments', controller: 'containments' %>
<%= form_for @order do |f| %>
// order fields
<%= f.fields_for :customer do |customer| %>
// customer fields
<% end %>
<% end %>
<%= form_for @order do |f| %>
Your info:
<ul>
<%= f.fields_for :customer do |customer| %>
<li>
<%= customer.label :name %>
<%= customer.text_field :name %>
<%= customer.label :address %>
<%= customer.text_field :address %>
<%= customer.label :phone %>
<%= customer.text_field :phone %>
<%= customer.label :email %>
<%= customer.text_field :email %>
</li>
<% end %>
</ul>
<%= f.submit %>
<% end %>
Flavors:
<ul>
</ul>
<!-- <%= f.fields_for :flavor do |flavor_form| %>
<li>
<%= flavor_form.label :name, "Pick your flavor" %>
<%= flavor_form.collection_select :order_id, Flavor.all,:id,:name %>
<%= flavor_form.select :flavor, Flavor.all.map { |c| [c.name, c.id] }%>
</li>
<% end %> -->
class Order < ActiveRecord::Base
has_one :containment
has_one :flavor, :through => :containment
has_many :toppings
has_many :toppings, :through => :containment
has_one :customer
accepts_nested_attributes_for :customer, :containment, :flavor, :toppings
validates :name, presence: true,
allow_blank: false,
uniqueness: true
end
require 'pry'
class OrdersController < ApplicationController
require 'mandrill'
# order = Order::OrdersController.new
def index
@orders = Order.all
end
def new
@order = Order.new
@order.build_customer
# @order.build_containment
# @order.build_flavor
# @order.toppings.build
end
def show
@order = Order.find(params[:id])
end
def create
@order = Order.new(order_params)
@customer = Customer.new
if @order.save
# Emailer.order_email(order)
redirect_to order_path@order
else
render 'new'
end
end
private
def order_params
params.require(:order).permit(
:customer_id,
# :containtment_id,
# :flavor_id,
# :topping_id,
customers_attributes: [ :id, :name, :address, :phone, :email]#,
# containments_attributes: [:id, :name]
# flavors_attributes: [:id, :name],
# toppings_attributes: [:id, :name],
)
end
end
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150602055146) do
create_table "containments", force: :cascade do |t|
t.string "name"
t.string "image_url"
t.integer "order_id"
end
create_table "customers", force: :cascade do |t|
t.string "name"
t.text "address"
t.integer "phone"
t.string "email"
t.integer "order_id"
end
create_table "flavors", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
end
create_table "orders", force: :cascade do |t|
t.integer "customer_id"
t.integer "containment_id"
t.integer "flavor_id"
t.integer "topping_id"
end
create_table "toppings", force: :cascade do |t|
t.string "name"
t.string "image_url"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment