Commit a610ad20 authored by Jon Mountjoy's avatar Jon Mountjoy

rails generate scaffold Widgets name:string description:text stock:integer

parent 312d8123
......@@ -5,6 +5,7 @@ source 'https://rubygems.org'
gem 'rails', '4.1.4'
# Use postgresql as the database for Active Record
gem 'pg'
gem 'rails_12factor', group: :production
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
......
# 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://coffeescript.org/
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}
pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}
a {
color: #000;
&:visited {
color: #666;
}
&:hover {
color: #fff;
background-color: #000;
}
}
div {
&.field, &.actions {
margin-bottom: 10px;
}
}
#notice {
color: green;
}
.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff;
}
ul li {
font-size: 12px;
list-style: square;
}
}
// Place all the styles related to the Widgets controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class WidgetsController < ApplicationController
before_action :set_widget, only: [:show, :edit, :update, :destroy]
# GET /widgets
# GET /widgets.json
def index
@widgets = Widget.all
end
# GET /widgets/1
# GET /widgets/1.json
def show
end
# GET /widgets/new
def new
@widget = Widget.new
end
# GET /widgets/1/edit
def edit
end
# POST /widgets
# POST /widgets.json
def create
@widget = Widget.new(widget_params)
respond_to do |format|
if @widget.save
format.html { redirect_to @widget, notice: 'Widget was successfully created.' }
format.json { render :show, status: :created, location: @widget }
else
format.html { render :new }
format.json { render json: @widget.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /widgets/1
# PATCH/PUT /widgets/1.json
def update
respond_to do |format|
if @widget.update(widget_params)
format.html { redirect_to @widget, notice: 'Widget was successfully updated.' }
format.json { render :show, status: :ok, location: @widget }
else
format.html { render :edit }
format.json { render json: @widget.errors, status: :unprocessable_entity }
end
end
end
# DELETE /widgets/1
# DELETE /widgets/1.json
def destroy
@widget.destroy
respond_to do |format|
format.html { redirect_to widgets_url, notice: 'Widget was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_widget
@widget = Widget.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def widget_params
params.require(:widget).permit(:name, :description, :stock)
end
end
module WidgetsHelper
end
class Widget < ActiveRecord::Base
end
<%= form_for(@widget) do |f| %>
<% if @widget.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@widget.errors.count, "error") %> prohibited this widget from being saved:</h2>
<ul>
<% @widget.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :stock %><br>
<%= f.number_field :stock %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<h1>Editing widget</h1>
<%= render 'form' %>
<%= link_to 'Show', @widget %> |
<%= link_to 'Back', widgets_path %>
<h1>Listing widgets</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Stock</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @widgets.each do |widget| %>
<tr>
<td><%= widget.name %></td>
<td><%= widget.description %></td>
<td><%= widget.stock %></td>
<td><%= link_to 'Show', widget %></td>
<td><%= link_to 'Edit', edit_widget_path(widget) %></td>
<td><%= link_to 'Destroy', widget, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Widget', new_widget_path %>
json.array!(@widgets) do |widget|
json.extract! widget, :id, :name, :description, :stock
json.url widget_url(widget, format: :json)
end
<h1>New widget</h1>
<%= render 'form' %>
<%= link_to 'Back', widgets_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Name:</strong>
<%= @widget.name %>
</p>
<p>
<strong>Description:</strong>
<%= @widget.description %>
</p>
<p>
<strong>Stock:</strong>
<%= @widget.stock %>
</p>
<%= link_to 'Edit', edit_widget_path(@widget) %> |
<%= link_to 'Back', widgets_path %>
json.extract! @widget, :id, :name, :description, :stock, :created_at, :updated_at
Rails.application.routes.draw do
resources :widgets
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
......
class CreateWidgets < ActiveRecord::Migration
def change
create_table :widgets do |t|
t.string :name
t.text :description
t.integer :stock
t.timestamps
end
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: 20140707111715) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "widgets", force: true do |t|
t.string "name"
t.text "description"
t.integer "stock"
t.datetime "created_at"
t.datetime "updated_at"
end
end
require 'test_helper'
class WidgetsControllerTest < ActionController::TestCase
setup do
@widget = widgets(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:widgets)
end
test "should get new" do
get :new
assert_response :success
end
test "should create widget" do
assert_difference('Widget.count') do
post :create, widget: { description: @widget.description, name: @widget.name, stock: @widget.stock }
end
assert_redirected_to widget_path(assigns(:widget))
end
test "should show widget" do
get :show, id: @widget
assert_response :success
end
test "should get edit" do
get :edit, id: @widget
assert_response :success
end
test "should update widget" do
patch :update, id: @widget, widget: { description: @widget.description, name: @widget.name, stock: @widget.stock }
assert_redirected_to widget_path(assigns(:widget))
end
test "should destroy widget" do
assert_difference('Widget.count', -1) do
delete :destroy, id: @widget
end
assert_redirected_to widgets_path
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
description: MyText
stock: 1
two:
name: MyString
description: MyText
stock: 1
require 'test_helper'
class WidgetsHelperTest < ActionView::TestCase
end
require 'test_helper'
class WidgetTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment