Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
% bin/rails generate scaffold document title:text content:text
- Loading branch information
Showing
20 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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://coffeescript.org/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the documents controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
body { | ||
background-color: #fff; | ||
color: #333; | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
line-height: 18px; | ||
margin: 33px; | ||
} | ||
|
||
p, ol, ul, td { | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
line-height: 18px; | ||
margin: 33px; | ||
} | ||
|
||
pre { | ||
background-color: #eee; | ||
padding: 10px; | ||
font-size: 11px; | ||
} | ||
|
||
a { | ||
color: #000; | ||
|
||
&:visited { | ||
color: #666; | ||
} | ||
|
||
&:hover { | ||
color: #fff; | ||
background-color: #000; | ||
} | ||
} | ||
|
||
th { | ||
padding-bottom: 5px; | ||
} | ||
|
||
td { | ||
padding-bottom: 7px; | ||
padding-left: 5px; | ||
padding-right: 5px; | ||
} | ||
|
||
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: 0; | ||
background-color: #c00; | ||
color: #fff; | ||
} | ||
|
||
ul li { | ||
font-size: 12px; | ||
list-style: square; | ||
} | ||
} | ||
|
||
label { | ||
display: block; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
class DocumentsController < ApplicationController | ||
before_action :set_document, only: [:show, :edit, :update, :destroy] | ||
|
||
# GET /documents | ||
# GET /documents.json | ||
def index | ||
@documents = Document.all | ||
end | ||
|
||
# GET /documents/1 | ||
# GET /documents/1.json | ||
def show | ||
end | ||
|
||
# GET /documents/new | ||
def new | ||
@document = Document.new | ||
end | ||
|
||
# GET /documents/1/edit | ||
def edit | ||
end | ||
|
||
# POST /documents | ||
# POST /documents.json | ||
def create | ||
@document = Document.new(document_params) | ||
|
||
respond_to do |format| | ||
if @document.save | ||
format.html { redirect_to @document, notice: 'Document was successfully created.' } | ||
format.json { render :show, status: :created, location: @document } | ||
else | ||
format.html { render :new } | ||
format.json { render json: @document.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /documents/1 | ||
# PATCH/PUT /documents/1.json | ||
def update | ||
respond_to do |format| | ||
if @document.update(document_params) | ||
format.html { redirect_to @document, notice: 'Document was successfully updated.' } | ||
format.json { render :show, status: :ok, location: @document } | ||
else | ||
format.html { render :edit } | ||
format.json { render json: @document.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /documents/1 | ||
# DELETE /documents/1.json | ||
def destroy | ||
@document.destroy | ||
respond_to do |format| | ||
format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_document | ||
@document = Document.find(params[:id]) | ||
end | ||
|
||
# Never trust parameters from the scary internet, only allow the white list through. | ||
def document_params | ||
params.require(:document).permit(:title, :content) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module DocumentsHelper | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Document < ApplicationRecord | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! document, :id, :title, :content, :created_at, :updated_at | ||
json.url document_url(document, format: :json) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_for(document) do |f| %> | ||
<% if document.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(document.errors.count, "error") %> prohibited this document from being saved:</h2> | ||
|
||
<ul> | ||
<% document.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= f.label :title %> | ||
<%= f.text_area :title %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= f.label :content %> | ||
<%= f.text_area :content %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= f.submit %> | ||
</div> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<h1>Editing Document</h1> | ||
|
||
<%= render 'form', document: @document %> | ||
<%= link_to 'Show', @document %> | | ||
<%= link_to 'Back', documents_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>Documents</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Content</th> | ||
<th colspan="3"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @documents.each do |document| %> | ||
<tr> | ||
<td><%= document.title %></td> | ||
<td><%= document.content %></td> | ||
<td><%= link_to 'Show', document %></td> | ||
<td><%= link_to 'Edit', edit_document_path(document) %></td> | ||
<td><%= link_to 'Destroy', document, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<br> | ||
|
||
<%= link_to 'New Document', new_document_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @documents, partial: 'documents/document', as: :document |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<h1>New Document</h1> | ||
|
||
<%= render 'form', document: @document %> | ||
<%= link_to 'Back', documents_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= @document.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Content:</strong> | ||
<%= @document.content %> | ||
</p> | ||
|
||
<%= link_to 'Edit', edit_document_path(@document) %> | | ||
<%= link_to 'Back', documents_path %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "documents/document", document: @document |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Rails.application.routes.draw do | ||
resources :documents | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateDocuments < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :documents do |t| | ||
t.text :title | ||
t.text :content | ||
|
||
t.timestamps | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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: 20161222023536) do | ||
|
||
create_table "documents", force: :cascade do |t| | ||
t.text "title" | ||
t.text "content" | ||
t.datetime "created_at", null: false | ||
t.datetime "updated_at", null: false | ||
end | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'test_helper' | ||
|
||
class DocumentsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@document = documents(:one) | ||
end | ||
|
||
test "should get index" do | ||
get documents_url | ||
assert_response :success | ||
end | ||
|
||
test "should get new" do | ||
get new_document_url | ||
assert_response :success | ||
end | ||
|
||
test "should create document" do | ||
assert_difference('Document.count') do | ||
post documents_url, params: { document: { content: @document.content, title: @document.title } } | ||
end | ||
|
||
assert_redirected_to document_url(Document.last) | ||
end | ||
|
||
test "should show document" do | ||
get document_url(@document) | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get edit_document_url(@document) | ||
assert_response :success | ||
end | ||
|
||
test "should update document" do | ||
patch document_url(@document), params: { document: { content: @document.content, title: @document.title } } | ||
assert_redirected_to document_url(@document) | ||
end | ||
|
||
test "should destroy document" do | ||
assert_difference('Document.count', -1) do | ||
delete document_url(@document) | ||
end | ||
|
||
assert_redirected_to documents_url | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
title: MyText | ||
content: MyText | ||
|
||
two: | ||
title: MyText | ||
content: MyText |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require 'test_helper' | ||
|
||
class DocumentTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |