LoginSignup
16

More than 5 years have passed since last update.

Railsでタスク管理アプリを作成

Last updated at Posted at 2014-04-13

タスク管理アプリを作ってみよう

1. 前置き

1. 前提・事前作業

2. railsアプリケーション作成

  • railsコマンドでアプリケーションを作成(ホームディレクトリにて実行)
  • オプションで --skip-bundleとすれば、ライブラリとかのインストールはスキップ可能
rails new taskApp
  • アプリケーションのルートフォルダへ移動
cd taskApp
  • Gemfileの編集
vi Gemfile
  • therubyracerを有効化(下記コメントアウトを有効化)
# gem 'therubyracer',  platforms: :ruby
  • 起動チェック
rails s
  • OK

3. プロジェクトの作成

  • プロジェクト・・・RailsやRuby的な用語と言うより、このアプリの管理単位(タスク)
  • scaffoldではなく、手作業で作成
  • 下記を作成する
    • model
    • controller
  • rails generate コマンドにて作成(generateg で代用可能)

4. modelの作成

  • Projectの属性としてtitleを持たせる
  • modelの名称は 先頭は大文字単数形 とすること(CoC)
rails g model Project title:string
  • マイグレーションファイルが生成されるので、rake で反映させる
rake db:migrate
  • チェックしてみる
  • 今使ってるDBへアクセス
rails db
  • sqliteだった。
sqlite3
.schema
  • projectsっていうテーブルが出来てた。
  • id っていうカラムがPKになってた。
  • title っていうカラムも当然あった。
  • 今のところ当然データはなし
sqlite3
select count(*) from projects;
  • 脱出
sqlite3
.exit
  • もうひとつの管理機能
  • 作成したmodelをインタラクティブに弄っている、らしい。
rails console
  • 弄ってみる
  • save 実行時にPKである id にも値が登録される(Insert文が走る)
ruby
p = Project.new(title:"project1") # 必要な情報のみ登録
p.save                            # セーブ
p                                 # 登録内容を確認
  • new と `save' を一緒にやりたければ下記の通り
ruby
Project.create(title:"project2")
  • p の代わりは以下の通り(select文を直接発行するっぽい)
ruby
Project.all

5. controllerの作成

  • modelと似たような感じ
  • modelに対するcontrollerを作成時は 先頭大文字複数形 がルール

    rails g controller Projects

  • ↓な感じのメッセージが表示される

      create  app/controllers/projects_controller.rb
      invoke  erb
      create    app/views/projects
      invoke  test_unit
      create    test/controllers/projects_controller_test.rb
      invoke  helper
      create    app/helpers/projects_helper.rb
      invoke    test_unit
      create      test/helpers/projects_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/projects.js.coffee
      invoke    scss
      create      app/assets/stylesheets/projects.css.scss
  • /home/vagrant/taskApp/app/controllers にある application_controller.vb を開いてみる。
ruby
class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
end
  • 同じく /home/vagrant/taskApp/config にある routes.vb を開いてみる。
ruby
Rails.application.routes.draw do


  # 追加!!
  resources :projects # Projectに関するルーティングを自動生成


  # The priority is based upon order of creation: first created -> highest priority.
  # See how all your routes lay out with "rake routes".

  # You can have the root of your site routed with "root"
  # root 'welcome#index'

  # Example of regular route:
  #   get 'products/:id' => 'catalog#view'

  # Example of named route that can be invoked with purchase_url(id: product.id)
  #   get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

  # Example resource route (maps HTTP verbs to controller actions automatically):
  #   resources :products

  # Example resource route with options:
  #   resources :products do
  #     member do
  #       get 'short'
  #       post 'toggle'
  #     end
  #
  #     collection do
  #       get 'sold'
  #     end
  #   end

  # Example resource route with sub-resources:
  #   resources :products do
  #     resources :comments, :sales
  #     resource :seller
  #   end

  # Example resource route with more complex sub-resources:
  #   resources :products do
  #     resources :comments
  #     resources :sales do
  #       get 'recent', on: :collection
  #     end
  #   end

  # Example resource route with concerns:
  #   concern :toggleable do
  #     post 'toggle'
  #   end
  #   resources :posts, concerns: :toggleable
  #   resources :photos, concerns: :toggleable

  # Example resource route within a namespace:
  #   namespace :admin do
  #     # Directs /admin/products/* to Admin::ProductsController
  #     # (app/controllers/admin/products_controller.rb)
  #     resources :products
  #   end
end
  • 編集結果をサーバにアップしたら rake でルーティングを確認
rake routes
  • こんなんが出る
(in /home/vagrant/taskApp)
      Prefix Verb   URI Pattern                  Controller#Action
    projects GET    /projects(.:format)          projects#index
             POST   /projects(.:format)          projects#create
 new_project GET    /projects/new(.:format)      projects#new
edit_project GET    /projects/:id/edit(.:format) projects#edit
     project GET    /projects/:id(.:format)      projects#show
             PATCH  /projects/:id(.:format)      projects#update
             PUT    /projects/:id(.:format)      projects#update
             DELETE /projects/:id(.:format)      projects#destroy

一旦ポスト
続きは次の投稿で書く

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
16