Permalink
Browse files

Scaffolded Orders

  • Loading branch information...
1 parent 9838e0b commit 7d4fe371d30e6f62e1efbfc6310663c077337142 @florida committed Nov 8, 2012
@@ -0,0 +1,3 @@
+# 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://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the orders controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,83 @@
+class OrdersController < ApplicationController
+ # GET /orders
+ # GET /orders.json
+ def index
+ @orders = Order.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @orders }
+ end
+ end
+
+ # GET /orders/1
+ # GET /orders/1.json
+ def show
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @order }
+ end
+ end
+
+ # GET /orders/new
+ # GET /orders/new.json
+ def new
+ @order = Order.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @order }
+ end
+ end
+
+ # GET /orders/1/edit
+ def edit
+ @order = Order.find(params[:id])
+ end
+
+ # POST /orders
+ # POST /orders.json
+ def create
+ @order = Order.new(params[:order])
+
+ 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
+
+ # PUT /orders/1
+ # PUT /orders/1.json
+ def update
+ @order = Order.find(params[:id])
+
+ respond_to do |format|
+ if @order.update_attributes(params[:order])
+ format.html { redirect_to @order, notice: 'Order was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @order.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /orders/1
+ # DELETE /orders/1.json
+ def destroy
+ @order = Order.find(params[:id])
+ @order.destroy
+
+ respond_to do |format|
+ format.html { redirect_to orders_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module OrdersHelper
+end
View
@@ -0,0 +1,3 @@
+class Order < ActiveRecord::Base
+ attr_accessible :address, :email, :first_name, :last_name
+end
@@ -0,0 +1,33 @@
+<%= 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 %>
+
+ <div class="field">
+ <%= f.label :first_name %><br />
+ <%= f.text_field :first_name %>
+ </div>
+ <div class="field">
+ <%= f.label :last_name %><br />
+ <%= f.text_field :last_name %>
+ </div>
+ <div class="field">
+ <%= f.label :email %><br />
+ <%= f.text_field :email %>
+ </div>
+ <div class="field">
+ <%= f.label :address %><br />
+ <%= f.text_field :address %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing order</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @order %> |
+<%= link_to 'Back', orders_path %>
@@ -0,0 +1,29 @@
+<h1>Listing orders</h1>
+
+<table>
+ <tr>
+ <th>First name</th>
+ <th>Last name</th>
+ <th>Email</th>
+ <th>Address</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @orders.each do |order| %>
+ <tr>
+ <td><%= order.first_name %></td>
+ <td><%= order.last_name %></td>
+ <td><%= order.email %></td>
+ <td><%= order.address %></td>
+ <td><%= link_to 'Show', order %></td>
+ <td><%= link_to 'Edit', edit_order_path(order) %></td>
+ <td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Order', new_order_path %>
@@ -0,0 +1,5 @@
+<h1>New order</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', orders_path %>
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>First name:</b>
+ <%= @order.first_name %>
+</p>
+
+<p>
+ <b>Last name:</b>
+ <%= @order.last_name %>
+</p>
+
+<p>
+ <b>Email:</b>
+ <%= @order.email %>
+</p>
+
+<p>
+ <b>Address:</b>
+ <%= @order.address %>
+</p>
+
+
+<%= link_to 'Edit', edit_order_path(@order) %> |
+<%= link_to 'Back', orders_path %>
View
@@ -1,4 +1,6 @@
Elmorfstore::Application.routes.draw do
+ resources :orders
+
resources :products
# The priority is based upon order of creation:
@@ -0,0 +1,12 @@
+class CreateOrders < ActiveRecord::Migration
+ def change
+ create_table :orders do |t|
+ t.string :first_name
+ t.string :last_name
+ t.string :email
+ t.string :address
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,13 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ first_name: MyString
+ last_name: MyString
+ email: MyString
+ address: MyString
+
+two:
+ first_name: MyString
+ last_name: MyString
+ email: MyString
+ address: MyString
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class OrdersControllerTest < ActionController::TestCase
+ setup do
+ @order = orders(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:orders)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create order" do
+ assert_difference('Order.count') do
+ post :create, order: { address: @order.address, email: @order.email, first_name: @order.first_name, last_name: @order.last_name }
+ end
+
+ assert_redirected_to order_path(assigns(:order))
+ end
+
+ test "should show order" do
+ get :show, id: @order
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, id: @order
+ assert_response :success
+ end
+
+ test "should update order" do
+ put :update, id: @order, order: { address: @order.address, email: @order.email, first_name: @order.first_name, last_name: @order.last_name }
+ assert_redirected_to order_path(assigns(:order))
+ end
+
+ test "should destroy order" do
+ assert_difference('Order.count', -1) do
+ delete :destroy, id: @order
+ end
+
+ assert_redirected_to orders_path
+ end
+end
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class OrdersHelperTest < ActionView::TestCase
+end
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class OrderTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end

0 comments on commit 7d4fe37

Please sign in to comment.