Rails勉強会資料

32
MTG デデデデデ デデデデデデデ

description

 

Transcript of Rails勉強会資料

Page 1: Rails勉強会資料

MTG デッキ構築アプリを作った

Page 2: Rails勉強会資料

自己紹介• サーバーサイドエンジニア

• 普段は主に Java を書いてます

• Ruby は個人的に使うスクリプトをたまに書く程度• 最近は chef を書いてます

Page 3: Rails勉強会資料

アプリをつくってみた

Page 4: Rails勉強会資料

Rails を使えばまるで魔法をつかっているかのように

簡単にアプリを作れる

Page 5: Rails勉強会資料

そんなわけなかった∑ (  ̄ Д  ̄; )

Page 6: Rails勉強会資料

Rais は僕を魔法使いにはしてくれない

Page 7: Rails勉強会資料

ただ、 Rails を使うとアプリを作るのが少し楽になる!

作るのが楽しくなる!

Page 8: Rails勉強会資料

MTG DECK BUILDER

Page 9: Rails勉強会資料

Rails を使ってもデザイン能力はあがらなかった

Page 10: Rails勉強会資料

ここから本題

Page 11: Rails勉強会資料

テーブルの管理が簡単

Page 12: Rails勉強会資料

migration を使ったテーブルのバージョン管理

テーブルにカラムを追加したり、テーブルからカラムを削除する テーブルをある時点の状態にする テーブルを最初から作り直す

自由にテーブル変更ができる。ただし、既に push した定義ファイルを修整してテーブルを更新するのは NG

Page 13: Rails勉強会資料

Model がきれいテーブルの各カラムに対応する変数をモデルないで再定義するなんて無駄なことをやらない

Playの例@Entitypublic class User extends Model {

public String email; public String password;

public User(String email, String password, String fullname) { this.email = email; this.password = password; }

}

Page 14: Rails勉強会資料

ちなみにRails では type というカラム名は予約語となっているらしく、使うことが出来ません。おこられます。しかも View を表示するときになってはじめて気づかされます。

The single-table inheritance mechanism failed to locate the subclass: ' インスタント '. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite MstCard.inheritance_column to use another column for that information.

カラム名として使えない名前は他にもあるので、気をつけてください!

Page 15: Rails勉強会資料

URL とアクションの紐付けが一カ所にまとまっているので見やすい

Page 16: Rails勉強会資料

earch_mst_cards GET /mst_cards/search(.:format) mst_cards GET /mst_cards(.:format) POST/mst_cards(.:format)new_mst_card GET /mst_cards/new(.:format) edit_mst_card GET /mst_cards/:id/edit(.:format) mst_card GET /mst_cards/:id(.:format)

PATCH /mst_cards/:id(.:format)

PUT /mst_cards/:id(.:format)

DELETE /mst_cards/:id(.:format)

resources :mst_cards do collection do get 'search' endend

設定を省略してかける

Page 17: Rails勉強会資料

デフォルトの設定をそのままつかっていいわけでもない

/mst_cards/search(.:format)

さすがにこの URL だと、テーブル名がバレバレなのでセキュリティ的に不安

規約だけに頼らずに、状況に応じて自分で設定を書くようにした方がよい!

Page 18: Rails勉強会資料

Ajax を簡単に使える

Page 19: Rails勉強会資料

View Action

JSON

View

レスポンス

リクエスト

Page 20: Rails勉強会資料

特別な処理をほとんど書かないでいい

<%= button_to " 増やす ", {:controller => "user", :action => "add_card”, :id => card.card_id}            , {:method => "POST", :remote => true} %>

:remote => true をつかうことで、 link や form の通信を非同期で行える。

:data-type を指定すれば、レスポンスを html 形式で取得することも可能

Index.html.erb

Page 21: Rails勉強会資料

def add_card #カードの所持数を増やす処理

respond_to do |format| format.js #renderを実行しない end end

user/add_card.js.erbvar dom = $(".card_count", "#card_<%= @user_card.card_id %>")var val = dom.text()val = Number(val) || 0val = val+1dom.text(val)

user_controller.rb

Page 22: Rails勉強会資料

Index_htm.erb

user_control.rbadd_card アクショ

Add_card.js.erb

add_card.html.erb

レスポンス

リクエスト

:remote => true を追加 出力を json に切り替える変更を加える点

Page 23: Rails勉強会資料

ヘルパーメソッドを作って独自の表示をする

Page 24: Rails勉強会資料
Page 25: Rails勉強会資料

<%= apply_img_and_hbr card.mana %>

def apply_img_and_hbr (str) apply_mana_img(str_br).html_safe end #{}で囲まれた文字列を imgタグに置換する def apply_mana_img (str) str.gsub('{1}', mana_img("1")) .gsub(/{U}/, mana_img("U")) .gsub(/{X}/, mana_img("X")) .gsub(/{Tap}/, mana_img("Tap")) end

def mana_img (word) options = {} options[:src] = "/images/#{word}.png" options[:alt] = "{#{word}}” tag("img", options) end

application_helper.rb に定義

Page 26: Rails勉強会資料

最後に

Page 27: Rails勉強会資料

Heroku

• Heroku にアカウント登録https://www.heroku.com/

 • git をインストール

Page 28: Rails勉強会資料

• Github にアカウント登録https://github.com/

• Github に SSH Key を登録サイト右上の「 account setting 」をクリック。SSH Keys を選択します。自分の SSH Key を登録します。

• Github にレポジトリを作成サイト右上の「 git a new repo 」から新しいレポジトリを作成します。レポジトリ名を入力して完了ボタンを押します。登録完了画面に表示される「 Push an existing repository from the command line 」以下のコマンドを自分の PC で実行します。

git remote add origin [URL]git add .git commit –m “first commit”git push –u origin master

Page 29: Rails勉強会資料

• Heroku にアプリケーションを登録下記のサイトを参考に toolbelt をインストールします。https://toolbelt.heroku.com/

Getting Start に従って Heroku にアプリの格納場所を作成します。

.git/confi に以下の設定が追加されてれば OK[remote "heroku"] url = [HEROKU_URL] fetch = +refs/heads/*:refs/remotes/heroku/*

Page 30: Rails勉強会資料

以下のコマンドを実行して Heroku にアプリを登録します。

git push heroku master

ちなみに、 Heroku では sqlite3 と mysql をサポートしていないため、 heroku にアップするときには posgresql を使うようにします。

Gemfile group :production do gem "pg" end group :development, :test do gem "sqlite3" end

また Heroku で postgresql を使用す場合は Heroku のpostgresql プラグインをインストールする必要があります。インストールは heroku のページから可能です。

Page 31: Rails勉強会資料

• Heroku を再起動

Migration の実行  heroku run rake db:migrate

サーバの再起動  herokuk restart

ブラウザで起動  heroku open  ブラウザが起動して、アプリのページにアクセスします。

Page 32: Rails勉強会資料

まとめ• Migration をつかって DB のバージョン管理。

• URL とアクションの紐付けは routes.rb で行う。必要に応じて設定も書く。

• Ajax の処理は簡単にかける。うまく設計すればアプリの APIを提供することも  簡単に出来る。

• ヘルパーメソッドを自作すれば、様々な表示ができる。また再利用したいメソッドを複数あつめて Utility モジュールをつくることもできる。