LoginSignup
45
42

More than 5 years have passed since last update.

Ruby2.0とRails4でメモアプリを作る

Last updated at Posted at 2013-08-15

Rails開発環境の構築(rbenvでRuby導入からBundler、Rails導入まで)
http://qiita.com/emadurandal/items/a60886152a4c99ce1017

↑の環境構築は終わってるものとする。

まずはRuby2.0をインストール(2系は前から使っていたのだけど、RVMからrbenvに移行したので消えていました)

ant% rbenv install 2.0.0-p247
Downloading ruby-2.0.0-p247.tar.gz...
-> ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
7Installing ruby-2.0.0-p247...
Installed ruby-2.0.0-p247 to /Users/toyoshi/.rbenv/versions/2.0.0-p247

ついでなのでデフォルトを2にします

ant% rbenv global 2.0.0-p247
ant% ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0]

gemはbundlerで管理するのでbundlerをいれる

ant% rbenv exec gem install bundler
Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
Installing ri documentation for bundler-1.3.5
1 gem installed
ant% rbenv rehash
ant% gem list

*** LOCAL GEMS ***

bigdecimal (1.2.0)
bundler (1.3.5)
io-console (0.4.2)
json (1.7.7)
minitest (4.3.2)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
test-unit (2.0.0.0)

今回はrails4-memoというメモアプリを作ってみることにします。
まず、ディレクトリを作ってGit管理を始めます

ant% mkdir rails4-memo
ant% cd rails4-memo
ant% git init
Initialized empty Git repository in /Users/toyoshi/projects/rails4-memo/.git/

そしたらbundle init

ant% bundle init
Writing new Gemfile to /Users/toyoshi/projects/rails4-memo/Gemfile
ant% ls
Gemfile
ant% cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

# gem "rails"

Gemfileを書き換えてRails4を指定する

ant% cat Gemfile
# A sample Gemfile
source "https://rubygems.org"

gem "rails", "4.0.0"

そしたらRailsをインストール

ant% bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...

省略

Installing tilt (1.4.1)
Installing sprockets (2.10.0)
Installing sprockets-rails (2.0.0)
Installing rails (4.0.0)
Your bundle is complete!
It was installed into ./vendor/bundle

あとはカレントにRailsのプロジェクトを展開

ant% bundle exec rails new .

早速サーバを立ち上げます。

ant% bundle exec rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-08-15 22:16:32] INFO  WEBrick 1.3.1
[2013-08-15 22:16:32] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin12.4.0]
[2013-08-15 22:16:32] INFO  WEBrick::HTTPServer#start: pid=25294 port=3000

http://localhost:3000/
にアクセスして確認。Rails4からはこのスタートページが動的に生成されpublic/index.htmlの削除が不要になったそうだ。

herokuにデプロイ

herokuのアカウント登録とheroku toolbeltのインストールは終わっているものとします。herokuのgemもありますが、そちらは今は推奨されていないようです。

ant% heroku create
Creating young-mesa-7373... done, stack is cedar
http://young-mesa-7373.herokuapp.com/ | git@heroku.com:young-mesa-7373.git
Git remote heroku added
ant% heroku apps:rename rails4memo
Renaming young-mesa-7373 to rails4memo... done
http://rails4memo.herokuapp.com/ | git@heroku.com:rails4memo.git
Git remote heroku updated

途中でリネームしましたが、最初から名前を指定することも出来ます。
終わったらいよいよデプロイです
(Gemfileを書き換えて、sqlite3はdevelopmentとtestだけ、productionではpgを使うようにしておきます)

ant% git push heroku master

Gemを入れる

見た目がひどすぎるとモチベーションに影響がでるのでbootstrap3のgemをいれます
https://github.com/anjlab/bootstrap-rails

generate

とりあえずscaffoldしてみます。タイトルと本文があるだけの単純な構成にしました。

ant% bundle exec rails g scaffold page title:string body:text
      invoke  active_record
      create    db/migrate/20130816002815_create_pages.rb
      create    app/models/page.rb
      invoke    test_unit
      create      test/models/page_test.rb
      create      test/fixtures/pages.yml
      invoke  resource_route
       route    resources :pages
      invoke  scaffold_controller
      create    app/controllers/pages_controller.rb
      invoke    erb
      create      app/views/pages
      create      app/views/pages/index.html.erb
      create      app/views/pages/edit.html.erb
      create      app/views/pages/show.html.erb
      create      app/views/pages/new.html.erb
      create      app/views/pages/_form.html.erb
      invoke    test_unit
      create      test/controllers/pages_controller_test.rb
      invoke    helper
      create      app/helpers/pages_helper.rb
      invoke      test_unit
      create        test/helpers/pages_helper_test.rb
      invoke    jbuilder
      create      app/views/pages/index.json.jbuilder
      create      app/views/pages/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/pages.js.coffee
      invoke    scss
      create      app/assets/stylesheets/pages.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

テーブルを作ります

ant% bundle exec rake db:migrate
==  CreatePages: migrating ====================================================
-- create_table(:pages)
   -> 0.0013s
==  CreatePages: migrated (0.0014s) ===========================================

認証をつけます

deviseというライブラリをつかいます。

gem 'devise'

くわしくはこちら
http://railsgirls.jp/devise/

認可のしくみをつける

ログインしているユーザは作成・編集ができる、上級の権限をもっているユーザは削除ができるようにする
これはcancanというgemでできる

省略

45
42
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
45
42