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

second commit #1

Open
wants to merge 2 commits into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/tasks.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/tasks.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Tasks controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
53 changes: 53 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class TasksController < ApplicationController

before_action :new

def index
@tasks = Task.all
end

def show
@task = Task.find(params[:id])
end

def new
@task = Task.new
end

def create
@task = Task.new(task_params)
if @task.save
redirect_to tasks_path
else
flash.now[:alert] =@task.errors.full_messages
@tasks = Task.all
render :index
end
end

def edit
@task = Task.find(params[:id])
end

def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to tasks_path
else
flash.now[:alert] = @task.errors.full_messages
render :index
end
end

def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to tasks_path
end

private

def task_params
params.require(:task).permit(:title, :body)
end
end
2 changes: 2 additions & 0 deletions app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TasksHelper
end
6 changes: 6 additions & 0 deletions app/models/task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Task < ApplicationRecord

validates :title, presence: true
validates :body, presence: true

end
24 changes: 24 additions & 0 deletions app/views/tasks/_new_task.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>タスク登録</h1>

<% if flash["alert"].present? %>
<% flash["alert"].each do |value| %>
<%= content_tag(:div, value)%>
<% end %>
<%end%>

<%= form_with model: @task, local: true do |form| %>
<p>
タイトル<br>
<%= form.text_field :title %>
</p>

<p>
内容<br>
<%= form.text_area :body %>
</p>

<%= form.submit "登録" %>



<% end %>
24 changes: 24 additions & 0 deletions app/views/tasks/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1>変更</h1>

<% if flash["alert"].present? %>
<% flash["alert"].each do |value| %>
<%= content_tag(:div, value)%>
<% end %>
<%end%>

<%= form_with model: @task, local: true do |form| %>
<p>
タイトル<br>
<%= form.text_field :title %>
</p>

<p>
内容<br>
<%= form.text_area :body%>
</p>

<%= form.submit "更新" %>

<% end %>

<%= link_to "戻る", task_path(@task)%>
12 changes: 12 additions & 0 deletions app/views/tasks/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>タスク一覧</h1>


<ul>
<% @tasks.each do |task| %>
<li><%= link_to task.title, task_path(task) %> | <%= link_to "削除", task_path(task), method: :delete %></li>
<% end %>
</ul>

<hr>

<%= render partial: "new_task", locals: { message: @task } %>
25 changes: 25 additions & 0 deletions app/views/tasks/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>タスク登録_new</h1>

<ul>
<% if flash["alert"].present? %>
<font color= "red">【エラー内容】<br>
<% flash["alert"].each do |value| %>
<li><%= content_tag(:div, value)%></li>
<% end %>
<%end%>
</font><br>
</ul>

<%= form_with model: @task, local: true do |form| %>
<p>
タイトル<br>
<%= form.text_field :title %>
</p>

<p>
内容<br>
<%= form.text_area :body %>
</p>

<%= form.submit "登録" %>
<% end %>
15 changes: 15 additions & 0 deletions app/views/tasks/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>タスク詳細</h1>

<p>
<b>タイトル: <%= @task.title%> </b>
</p>

<p>
<b>内容: <%= @task.body %> </b>
</p>

<p>
<%= link_to "編集", edit_task_path(@task) %>
<%= link_to "削除", task_path(@task), method: :delete %>
<%= link_to "戻る", tasks_path(@task)%>
</p>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :tasks

end
10 changes: 10 additions & 0 deletions db/migrate/20180312125056_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateTasks < ActiveRecord::Migration[5.1]
def change
create_table :tasks do |t|
t.string :title
t.text :body

t.timestamps
end
end
end
22 changes: 22 additions & 0 deletions db/schema.rb
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: 20180312125056) do

create_table "tasks", force: :cascade do |t|
t.string "title"
t.text "body"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end
16 changes: 16 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,19 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)


Task.create(
title: "タスク1",
body: "タスク1の内容"
)

Task.create(
title: "タスク2",
body: "タスク2の内容"
)

Task.create(
title: "タスク3",
body: "タスク3の内容"
)
9 changes: 9 additions & 0 deletions test/controllers/tasks_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class TasksControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get tasks_index_url
assert_response :success
end

end
9 changes: 9 additions & 0 deletions test/fixtures/tasks.yml
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: MyString
body: MyText

two:
title: MyString
body: MyText
7 changes: 7 additions & 0 deletions test/models/task_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

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