elixir 教程
Okay, let’s get real for a second. No-one can actually build a production-ready blog application from scratch in less than 15 minutes. That’s not how the world works. However, creating a working prototype — that’s totally possible.
好吧,让我们成为现实。 实际上,没有人可以在不到15分钟的时间内从头开始构建可用于生产环境的博客应用程序。 这不是世界的运作方式。 但是,创建可行的原型是完全可能的。
So here is the plan — we are gonna spend the next 15min or so building a prototype of a blog application that meets the following criteria:
因此,这是计划—我们将在接下来的15分钟左右时间内构建符合以下条件的博客应用程序原型:
- User can create, update, display and delete posts 用户可以创建,更新,显示和删除帖子
- User can add comments to a post 用户可以在帖子中添加评论
- User can see all the comments for a particular post 用户可以查看特定帖子的所有评论
- User can see how many comments does each post have 用户可以看到每个帖子有多少条评论
A small side note — this blog post is inspired by an article I read a couple of days back — “Elixir Blog in 15 Minutes Using Phoenix Framework — Step By Step Tutorial” written by Jakub Cieślar. Since the article was published way back in 2015, I felt it will be nice to get an updated version that properly reflects the current state of Elixir and Phoenix.
一个小小的注释-这篇博客文章的灵感来自我几天前读到的一篇文章-由JakubCieślar撰写的“ 使用Phoenix Framework在15分钟之内的Elixir博客 —逐步教程” 。 自从该文章于2015年发布以来,我感到很高兴获得一个能够正确反映Elixir和Phoenix的当前状态的更新版本。
步骤0:先决条件 (STEP 0: Prerequisites)
The Phoenix Framework has a fantastic set of installation docs. I suggest you follow these and you’ll be set up and ready to go in no time.
Phoenix Framework提供了一套很棒的安装文档 。 我建议您遵循这些步骤,这样您就可以立即进行设置并准备就绪。
Another small side note — if you are planning to explore and work with different versions of Elixir in the future, you might want to install Elixir (and Erlang) via asdf.
另一个小注意事项-如果您打算将来探索和使用不同版本的Elixir,则可能要通过asdf安装Elixir(和Erlang)。
步骤1:创建博客应用程序 (STEP 1: Create the blog application)
First of all, meet your new best friend — Mix. This is an Elixir build tool that provides a basic set of tasks to help create and manage Elixir apps.
首先,结识新朋友-Mix 。 这是一个Elixir构建工具,提供了一组基本任务来帮助创建和管理Elixir应用程序。
To create a new application from scratch, all you need to do is open a new terminal window and enter the following command:
要从头开始创建新的应用程序,您需要做的就是打开一个新的终端窗口并输入以下命令:
mix phx.new blog
Halfway through the installation process you will be asked if you want to fetch and install the project dependancies — I suggest you go ahead and do so.
在安装过程的一半,系统将询问您是否要获取并安装项目相关性-我建议您继续进行。
* creating blog/config/config.exs
* creating blog/config/dev.exs
* creating blog/config/prod.exs
...
* creating blog/assets/css/phoenix.css
* creating blog/assets/static/images/phoenix.png
* creating blog/assets/static/robots.txtFetch and install dependencies? [Yn]
If you choose