スポンサーリンク

PhoenixTutorialを写経(1)

以下のサイトを写経してみる

RailsTutorial作者様、翻訳者様、以下のブログの作者様に本当に感謝!

2015年6月20日
[Rails Tutorial for Phoenix]Demo application
https://daruiapprentice.blogspot.jp/2015/06/rails-tutorial-for-phoenix_20.html

(環境)
Windows 8.1
VirtualBox 5.1.14
Vagrant 1.9.1
CentOS7.3
Erlang/OTP 19
Elixir 1.3.4
Phoenix 1.2.1

環境構築は以下のように行っている

http://twosquirrel.mints.ne.jp/?p=14421

WindowsでVagrant使って共有フォルダで操作するときの注意点

http://twosquirrel.mints.ne.jp/?p=14545

(0)Windows(ホストOS)のコマンドプロンプトで、vagrant upして、TeraTermでログイン(127.0.0.1, 2222, vagrant, vagrant)

image

(1)Preparation

mix phoenix.new demo_app

image image

cd demo_app
npm install --no-bin-links
node node_modules/brunch/bin/brunch build

image

mix ecto.create
mix phoenix.server

image image

image

ホストOSで、http://localhost:4000/

image

Ctrl +C を2回でサーバ停止。

(2)Data model

User
users
name:string, email:string
id:integer, inserted_at:timestamp, updated_at:timestamp

Micropost
microposts
content:string, user_id:integer
id:integer, inserted_at:timestamp, updated_at:timestamp

(3)Create users resource

mix phoenix.gen.html User users name:string email:string

image

web/router.ex

image

image image

mix ecto.migrate

image

mix phoenix.server

</>image

http://localhost:4000/users

image

UserのCRUD処理ができるようになった。(Railsのscaffoldと同じ。)

(4)Create microposts resource

mix phoenix.gen.html Micropost microposts content:string user_id:integer

web/router.ex

image

mix ecto.migrate

mix phoenix.server

image image

http://localhost:4000/microposts

image

web/models/micropost.ex
(元のページでは min になっていたので、 max の訂正した)

image

140文字以上のmicropostを投稿しようとすると、エラーが表示される

image image

(5)Associate with has_many

web/models/user.ex

image

web/models/micropost.ex

images

 

image

ん?よくわからない。

RailsTutorialでは、

https://railstutorial.jp/chapters/toy_app?version=5.0#sec-demo_user_has_many_microposts

のところではあるが、iexの使い方がいまいちわからないので、私にはuserモデルとmicropostモデルの関係は確認できず。

 

スポンサーリンク