尝试使用Elixir + Phoenix创建一个应用程序,它将能够处理"浏览器"和"api"请求以处理其资源.
是否可以这样做而不必做那样的事情:
scope "/", App do
pipe_through :browser
resources "/users", UserController
end
scope "/api", App.API as: :api do
pipe_through :api
resources "/users", UserController
end
Run Code Online (Sandbox Code Playgroud)
这意味着必须创建两个控制器,这些控制器可能具有相同的行为,除了它将使用浏览器管道呈现HTML ,例如JSON,用于api管道.
我想的可能就是Rails respond_to do |format| ...
我基本上都在寻找相当于Ruby的Elixir Array#sample.可以让我这样做的东西:
list = [1,2,3,4,5,6,7]
sample(list)
#=> 4
sample(list, 3)
#=> [6, 2, 5]
Run Code Online (Sandbox Code Playgroud)
我也没有在Elixir List Docs中找到任何内容.
来自Ruby的Elixir新手,我正在尝试使用以下代码打印出地图的数组值:
地图script.ex
list = [0, 1]
map = %{0 => [1, 2], 1 => [2, 3]}
Enum.each list, fn n ->
IO.puts map[n]
end
Run Code Online (Sandbox Code Playgroud)
输出:
^A^B
^B^C
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?Elixir看起来类似于Ruby,但它的表现不同......
它看到elixir有一个名为的工具,elixirc而erlang有一个工具,erlc用于编译模块以供使用.它在此之后立即说您可以使用elixir命令行工具运行代码.
有没有办法用elixir或erlang编译二进制可执行文件?(一个我可以chmod +x binary_name然后从同一个目录运行./binary_name)
我首先注意到一个带有尾随感叹号/爆炸(!)的函数,同时浏览Phoenix教程(在Incoming Events部分)
def handle_in("new_msg", %{"body" => body}, socket) do
broadcast! socket, "new_msg", %{body: body}
{:noreply, socket}
end
Run Code Online (Sandbox Code Playgroud)
尾随感叹号是什么意思?它有什么用吗?我一直在寻找并尝试寻找,但我不确定我是否使用了正确的条款.到目前为止,似乎只有作为约定的函数会在失败时引发错误,但总是意味着它总是意味着.
我看到它的唯一提及出现在Dave Thomas的"Programming Elixir"中:
Identifiers in Elixir are combinations of upper and lower case ASCII
characters, digits, and underscores. Function names may end with a
question mark or an exclamation point.
Run Code Online (Sandbox Code Playgroud)
而且在文档它提到:
Notice that when the file does not exist, the version with ! raises an
error. The version without ! is preferred when you want to handle …Run Code Online (Sandbox Code Playgroud) 如何在给定的ID列表中查找帖子?
这不起作用:
posts = Post |> where(id: [1, 2]) |> Repo.all
Run Code Online (Sandbox Code Playgroud)
Rails中的示例:
Post.where({ id: [1, 2]})
# SELECT * FROM posts WHERE id IN (1, 2)
Run Code Online (Sandbox Code Playgroud) 什么是查看数据库中条目数的最快方法?我想查看帖子/索引视图中的帖子数量.
假设我有一个Post模型和一堆保存在我的数据库中的帖子.在Rails中,我可以在视图文件中执行以下操作:
<h1><%= @posts.length %> Posts</h1>
Run Code Online (Sandbox Code Playgroud)
要么
<h1><%= @posts.size %> Posts</h1>
Run Code Online (Sandbox Code Playgroud)
要么
<h1><%= @posts.count %> Posts</h1>
Run Code Online (Sandbox Code Playgroud)
什么是凤凰城框架/ Elixir等同物?
我有一个清单:
a = [1,2,4,5,6,7,8,9,9,88,88]
Run Code Online (Sandbox Code Playgroud)
在Python中,获取最后n个项目很简单:
a[-n:]
Run Code Online (Sandbox Code Playgroud)
在Elixir中相当于什么?