Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

48-リレーションの調査と準備 #12

Open
wants to merge 1 commit into
base: 47
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/categories.coffee
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/categories.scss
@@ -0,0 +1,3 @@
// Place all the styles related to the categories controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
74 changes: 74 additions & 0 deletions app/controllers/categories_controller.rb
@@ -0,0 +1,74 @@
class CategoriesController < ApplicationController
before_action :set_category, only: [:show, :edit, :update, :destroy]

# GET /categories
# GET /categories.json
def index
@categories = Category.all
end

# GET /categories/1
# GET /categories/1.json
def show
end

# GET /categories/new
def new
@category = Category.new
end

# GET /categories/1/edit
def edit
end

# POST /categories
# POST /categories.json
def create
@category = Category.new(category_params)

respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render :show, status: :created, location: @category }
else
format.html { render :new }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /categories/1
# PATCH/PUT /categories/1.json
def update
respond_to do |format|
if @category.update(category_params)
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { render :show, status: :ok, location: @category }
else
format.html { render :edit }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end

# DELETE /categories/1
# DELETE /categories/1.json
def destroy
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_category
@category = Category.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(:name, :description)
end
end
2 changes: 2 additions & 0 deletions app/helpers/categories_helper.rb
@@ -0,0 +1,2 @@
module CategoriesHelper
end
2 changes: 2 additions & 0 deletions app/models/category.rb
@@ -0,0 +1,2 @@
class Category < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions app/views/categories/_category.json.jbuilder
@@ -0,0 +1,2 @@
json.extract! category, :id, :name, :description, :created_at, :updated_at
json.url category_url(category, format: :json)
25 changes: 25 additions & 0 deletions app/views/categories/_form.html.erb
@@ -0,0 +1,25 @@
<%= form_for(@category) do |f| %>
<% if @category.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>

<ul>
<% @category.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="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/categories/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing Category</h1>

<%= render 'form' %>

<%= link_to 'Show', @category %> |
<%= link_to 'Back', categories_path %>
29 changes: 29 additions & 0 deletions app/views/categories/index.html.erb
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Listing Categories</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @categories.each do |category| %>
<tr>
<td><%= category.name %></td>
<td><%= category.description %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Category', new_category_path %>
1 change: 1 addition & 0 deletions app/views/categories/index.json.jbuilder
@@ -0,0 +1 @@
json.array! @categories, partial: 'categories/category', as: :category
5 changes: 5 additions & 0 deletions app/views/categories/new.html.erb
@@ -0,0 +1,5 @@
<h1>New Category</h1>

<%= render 'form' %>

<%= link_to 'Back', categories_path %>
14 changes: 14 additions & 0 deletions app/views/categories/show.html.erb
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @category.name %>
</p>

<p>
<strong>Description:</strong>
<%= @category.description %>
</p>

<%= link_to 'Edit', edit_category_path(@category) %> |
<%= link_to 'Back', categories_path %>
1 change: 1 addition & 0 deletions app/views/categories/show.json.jbuilder
@@ -0,0 +1 @@
json.partial! "categories/category", category: @category
1 change: 1 addition & 0 deletions config/routes.rb
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :categories
root 'todos#index'

resources :todos do
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20160729082629_create_categories.rb
@@ -0,0 +1,10 @@
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.text :description

t.timestamps null: false
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160729064758) do
ActiveRecord::Schema.define(version: 20160729082629) do

create_table "categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "todos", force: :cascade do |t|
t.string "title"
Expand Down
49 changes: 49 additions & 0 deletions test/controllers/categories_controller_test.rb
@@ -0,0 +1,49 @@
require 'test_helper'

class CategoriesControllerTest < ActionController::TestCase
setup do
@category = categories(:one)
end

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:categories)
end

test "should get new" do
get :new
assert_response :success
end

test "should create category" do
assert_difference('Category.count') do
post :create, category: { description: @category.description, name: @category.name }
end

assert_redirected_to category_path(assigns(:category))
end

test "should show category" do
get :show, id: @category
assert_response :success
end

test "should get edit" do
get :edit, id: @category
assert_response :success
end

test "should update category" do
patch :update, id: @category, category: { description: @category.description, name: @category.name }
assert_redirected_to category_path(assigns(:category))
end

test "should destroy category" do
assert_difference('Category.count', -1) do
delete :destroy, id: @category
end

assert_redirected_to categories_path
end
end
9 changes: 9 additions & 0 deletions test/fixtures/categories.yml
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
description: MyText

two:
name: MyString
description: MyText
7 changes: 7 additions & 0 deletions test/models/category_test.rb
@@ -0,0 +1,7 @@
require 'test_helper'

class CategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end