harapeko / phoenix

phoenixを遊んで見る
0 stars 0 forks source link

scaffold的な事をしてCRUDページ作ってみる #2

Closed harapeko closed 7 years ago

harapeko commented 7 years ago

参考URLはここ Phoenix入門 #02

harapeko commented 7 years ago

mix phoenix.gen.htmlでscaffold的な事ができるぽい(MVC作成してくれる

harapeko commented 7 years ago
harapeko:~/workspace/hellow_phoenix (master) $ mix phoenix.gen.html Post posts title:string body:text
* creating web/controllers/post_controller.ex
* creating web/templates/post/edit.html.eex
* creating web/templates/post/form.html.eex
* creating web/templates/post/index.html.eex
* creating web/templates/post/new.html.eex
* creating web/templates/post/show.html.eex
* creating web/views/post_view.ex
* creating test/controllers/post_controller_test.exs
* creating web/models/post.ex
* creating test/models/post_test.exs
* creating priv/repo/migrations/20161101075255_create_post.exs

Add the resource to your browser scope in web/router.ex:

    resources "/posts", PostController

Remember to update your repository by running migrations:

    $ mix ecto.migrate

してあげた

harapeko commented 7 years ago

ログにあるようにルーティング追加してあげた resources "/posts", PostController

harapeko commented 7 years ago

続いてmigrateしてみた mix ecto.migrate

Compiling 9 files (.ex)
Generated hellow_phoenix app

08:08:59.035 [info]  == Running HellowPhoenix.Repo.Migrations.CreatePost.change/0 forward

08:08:59.035 [info]  create table posts

08:08:59.062 [info]  == Migrated in 0.0s
harapeko commented 7 years ago

追加されたposts table確認してみた

$ psql
ubuntu=# \l
                                  List of databases
        Name        |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
--------------------+----------+-----------+---------+-------+-----------------------
 hellow_phoenix_dev | postgres | UTF8      | C       | C     | 
 postgres           | postgres | SQL_ASCII | C       | C     | 
 template0          | postgres | SQL_ASCII | C       | C     | =c/postgres          +
                    |          |           |         |       | postgres=CTc/postgres
 template1          | postgres | SQL_ASCII | C       | C     | =c/postgres          +
                    |          |           |         |       | postgres=CTc/postgres
 ubuntu             | ubuntu   | SQL_ASCII | C       | C     | 
(5 rows)

ubuntu=# \c hellow_phoenix_dev 
You are now connected to database "hellow_phoenix_dev" as user "ubuntu".
hellow_phoenix_dev=# \dt
               List of relations
 Schema |       Name        | Type  |  Owner   
--------+-------------------+-------+----------
 public | posts             | table | postgres
 public | schema_migrations | table | postgres
(2 rows)

hellow_phoenix_dev=# \d posts
                                      Table "public.posts"
   Column    |            Type             |                     Modifiers                      
-------------+-----------------------------+----------------------------------------------------
 id          | integer                     | not null default nextval('posts_id_seq'::regclass)
 title       | character varying(255)      | 
 body        | text                        | 
 inserted_at | timestamp without time zone | not null
 updated_at  | timestamp without time zone | not null
Indexes:
    "posts_pkey" PRIMARY KEY, btree (id)
harapeko commented 7 years ago

一つだけ注意しときたいなと思ったのが、タイムスタンプ。 Railsでは生成されるフィールドがcreated_at, updated_atだったが、 Phoenixではinserted_at, updated_atだった。

harapeko commented 7 years ago

とりあえず新規作成、編集、削除、表示、戻るボタンなどを試したのでclose!