LoginSignup
0
2

More than 3 years have passed since last update.

rails todo

Posted at

Railsで簡単なTodoサービスを作ったった

機能

この四つを実装していく
- 一覧表示
- 登録
- 更新
- 削除

  1. アプリケーションファイルを作成してrailsが起動させる
ターミナル
$ rails new app //app ファイルを作成
$ cd app //作成したファイルに移動
$ rails s //サーバーを起動

http://localhost:3000/ にアクセスして『Yay! You’re on Rails!』が見れることを確認しよう

2.DBを作成する

コンソール
$ rails g model todo execution:text content:text memo:text
$ rails db:migrate
$ rails c

irb(main):001:0> task = Todo.new(execution:'1月1日',content:'水族館',memo:'お弁当')
irb(main):002:0> task.save

3.routes.rbを設定していく

Rails.application.routes.draw do
  get "/" => "home#top"
  get "home/new" => "home#new"
  post "home/create" => "home#create"
  get "home/:id/details" => "home#details"
  get "home/:id/edit" => "home#edit"
  post "home/:id/update" => "home#update"
  post "home/:id/destroy" => "home#destroy"
end

4.Controllerの設定

class HomeController < ApplicationController
    def top
        @task = Todo.all
    end

    def new
    end

    def create
       @task = Todo.new(execution: params[:execution],content: params[:content],memo: params[:memo])
       @task.save
       redirect_to("/") 
    end

    def details
        @task = Todo.find_by(id: params[:id])
    end

    def edit
        @task = Todo.find_by(id: params[:id])
    end

    def update
        @task = Todo.find_by(id: params[:id])
        @task.execution = params[:execution]
        @task.content = params[:content]
        @task.memo = params[:memo]
        @task.save
        redirect_to("/")
    end

    def destroy
        @task = Todo.find_by(id: params[:id])
        @task.destroy
        redirect_to("/")
    end
end

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2