Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用

Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用,第1张

概述本文通过一个书单应用简要介绍使用Phoenix框架创建一个Web应用程序的基本步骤. 安装Phoenix 1 2

本文通过一个书单应用简要介绍使用Phoenix框架创建一个Web应用程序的基本步骤.

安装Phoenix
             1                                                                   2                                                                   3                                                                   4      
             git clone http        s://github.        com/phoenixframework/phoenix.git                                                                   cd phoenix                                                                   git checkout v0.        5.0                                                                   mix        do deps.        get,compile      
创建书单项目

在phoenix源代码目录中运行如下命令创建一个phoenix项目目录结构

             1      
             mix phoenix.        new book_store ../book_store      

目录../book_store不必事先存在,phoenix会自动创建.

添加依赖库

编辑mix.exs文件,修改后如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9                                                                   10                                                                   11                                                                   12                                                                   13                                                                   14                                                                   15                                                                   16                                                                   17                                                                   18                                                                   19                                                                   20                                                                   21                                                                   22                                                                   23                                                                   24                                                                   25                                                                   26                                                                   27      
             defmodule BookStore.Mixfile         do                                                                   use        Mix.Project                                                                   def project         do                                                                   [        app:        :book_store,                                                             version:        "0.0.1",                                                             elixir:        "~> 1.0",                                                             elixirc_paths: [        "lib","web"],                                                             compilers: [        :phoenix] ++        Mix.compilers,                                                             deps: deps]                                                                   end                                                                   # Configuration for the OTP application                                                                   #                                                                   # Type `mix help compile.app` for more information                                                                   def application         do                                                                   [        mod: {        BookStore, []},                                                             applications: [        :phoenix,:cowboy,:logger,:postgrex,:ecto]]                                                                   end                                                                   # SpecifIEs your project dependencIEs                                                                   #                                                                   # Type `mix help deps` for examples and options                                                                   defp deps        do                                                                   [{        :phoenix,"0.5.0"},                                                             {        :cowboy,"~> 1.0"},                                                             {        :postgrex,"~> 0.5"},                                                             {        :ecto,"~> 0.2.0"}]                                                                   end                                                                   end      

和修改之前的mix.exs文件相比有两个变更处:

在application函数中增加了两个依赖的应用程序:postgres和:ecto(16行) 在deps函数增加两个依赖库{:postgrex,"~> 0.5"}和{:ecto,"~> 0.2.0"}(24,25行)

运行

             1      
             mix        do deps.        get,compile      
创建一个仓库(Repo)

创建文件web/models/repo.ex,内容如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9      
             defmodule BookStore.Repo         do                                                                   use        Ecto.Repo,        adapter:        Ecto.Adapters.Postgres                                                                   def conf         do                                                                   parse_url        "ecto://postgres:postgres@localhost/book_store"                                                                   end                                                                   def priv         do                                                                   app_dir(        :book_store,"priv/repo")                                                                   end                                                                   end      

创建数据库:

             1      
             createdb book_store -U postgres --enCoding='utf-        8' --locale=en_US.        UTF-        8 --        template=template0      

修改lib/book_store.ex为,如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9                                                                   10                                                                   11                                                                   12                                                                   13                                                                   14      
             defmodule BookStore         do                                                                   use        Application                                                                   # See http://elixir-lang.org/docs/stable/elixir/Application.HTML                                                                   # for more information on OTP Applications                                                                   def start(_type, _args)         do                                                                   import        Supervisor.Spec,        warn:        false                                                                   children = [                                                                   # define workers and child supervisors to be supervised                                                                   worker(        BookStore.Repo, [])                                                                   ]                                                                   opts = [        strategy:        :one_for_one,name:        BookStore.Supervisor]                                                                   Supervisor.start_link(children,opts)                                                                   end                                                                   end      

编译

             1      
             mix compile      
创建模型

创建文件web/models/books.ex,内容如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9      
             defmodule BookStore.Books         do                                                                   use        Ecto.Model                                                                   schema        "books"        do                                                                   fIEld        :Title,:string                                                                   fIEld        :description,:string                                                                   fIEld        :author,:string                                                                   fIEld        :publisher,:string                                                                   end                                                                   end      
创建数据库移植脚本
             1                                                                   2                                                                   3                                                                   4                                                                   5      
             $ mix ecto.gen.migration Bookstore.Repo create_book                                                                   Compiled web        /models/books.ex                                                                   Generated bookstore.app                                                                   * creating priv        /repo/migrations                                                                   * creating priv        /repo/migrations        /20141112170140_create_book.exs      

编辑生成的priv/repo/migrations/20141112170140_create_book.exs脚本,内容如下:

defmodule BookStore.Repo.Migrations.CreateBook do use Ecto.Migration def up do ["CREATE table books(\ ID serial primary key,\ Title varchar(125),\ description text,\ author varchar(255),\ publisher varchar(255))",\ "INSERT INTO books(Title,description,author,publisher) \ VALUES ( \ 'Programming Elixir',\ 'Programming Elixir: Functional |> Concurrent |> Pragmatic |> Fun',\ 'Dave Thomas',\ 'The Pragmatic Bookshelf')" ] end def down do "DROP table books" end end

运行移植脚本

             1      
             mix ecto.migrate BookStore.Repo      
创建查询

创建文件web/models/querIEs.ex,内容如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8      
             defmodule BookStore.QuerIEs        do                                                                   import Ecto.        query                                                                   def books_query        do                                                                   query =        from book        in BookStore.Books,                                                             select: book                                                                   BookStore.Repo.        all(        query)                                                                   end                                                                   end      
配置路由

打开文件web/router.ex,修改为如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9                                                                   10                                                                   11                                                                   12                                                                   13      
             defmodule BookStore.Router        do                                                                   use Phoenix.Router                                                                   scope        "/"        do                                                                   #        Use the        default browser stack.                                                                   pipe_through :browser                                                                   #        get        "/",BookStore.PageController,:        index,as: :pages                                                                   get        "/",BookStore.BookController,as: :books                                                                   end                                                                   # Other scopes may        use custom stacks.                                                                   # scope        "/API"        do                                                                   # pipe_through :API                                                                   #        end                                                                   end      
创建控制器

创建文件web/controllers/book_controller.ex,内容如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8      
             defmodule BookStore.BookController         do                                                                   use        Phoenix.Controller                                                                   plug        :action                                                                   def index(conn,_params)         do                                                                   books =        BookStore.QuerIEs.books_query                                                                   render conn,"index",books: books                                                                   end                                                                   end      
创建书单视图

创建文件web/vIEws/book_vIEw.ex,内容如下:

             1                                                                   2                                                                   3      
             defmodule BookStore.BookVIEw         do                                                                   use        BookStore.VIEws                                                                   end      

创建目录

             1      
             mkdir web/templates/book      

并添加文件web/templates/book/index.HTML.eex,内容如下:

             1                                                                   2                                                                   3                                                                   4                                                                   5                                                                   6                                                                   7                                                                   8                                                                   9                                                                   10                                                                   11                                                                   12                                                                   13                                                                   14                                                                   15                                                                   16                                                                   17                                                                   18                                                                   19                                                                   20                                                                   21                                                                   22                                                                   23      
             <h1>我的图书        </h1>                                                                   <table class='table table-bodered table-striped'>                                                                   <thead>                                                                   <tr>                                                                   <th>#        </th>                                                                   <th>标题        </th>                                                                   <th>描述        </th>                                                                   <th>作者        </th>                                                                   <th>出版社        </th>                                                                   </tr>                                                                   </thead>                                                                   <tbody>                                                                   <%= for book <- @books do %>                                                                   <tr>                                                                   <td>        <%= book.ID %>        </td>                                                                   <td>        <%= book.Title %>        </td>                                                                   <td>        <%= book.description %>        </td>                                                                   <td>        <%= book.author %>        </td>                                                                   <td>        <%= book.publisher %>        </td>                                                                   </tr>                                                                   <% end %>                                                                   </tbody>                                                                   </table>      

启动应用,并刷新页面

             1      
             mix phoenix.start      

我的书单应用

完成!

参考资料 Book Listing App With Elixir,Phoenix,Postgres and Ecto
http://learnelixir.com/blog/2014/10/05/build-web-app-with-elixir/ 总结

以上是内存溢出为你收集整理的Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用全部内容,希望文章能够帮你解决Elixir 用Phoenix,Postgresql和Ecto创建一个书单应用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176506.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存