标签: elixir

从 Elixir 的视图中调用渲染部分

我正在和 Phoenix 一起学习 Elixir,但陷入了一个相当愚蠢的境地。我想通过以下方式从索引模板内部调用部分的渲染:

#index.html.slim

- for element_here  <- array_here do
  = render MyApp.SharedView, "_game.html.slim", element: element_here
Run Code Online (Sandbox Code Playgroud)

为此,我创建了一个名为 shared_view.ex 的视图,如下所示:

defmodule MyApp.SharedView do
  use MyApp.Web, :view
  def render("game", _assigns), do: "/shared/_game.html.slim"
end
Run Code Online (Sandbox Code Playgroud)

我预计它会经历循环渲染共享/_game.html.slim,我将其复制到此处:

.col-md-4.portfolio-item
  a href="#"
    img.img-responsive alt="" src="http://placehold.it/700x400" /
  h3
    a href="#"  Project Name
  p Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.
Run Code Online (Sandbox Code Playgroud)

但没有任何内容被渲染。我也没有收到错误。它只是渲染之前和之后的内容。

我不确定我在这里缺少什么。没有路线或控制器操作连接到“_game”部分,因为我认为它没有必要(我习惯了 Rails,它在那里以这种方式工作)。

rendering elixir phoenix-framework

0
推荐指数
1
解决办法
1176
查看次数

Elixir/Phoenix 在 ErrorView 上渲染 json

当路由不匹配时,我尝试发送 json。从error_view.ex,我认为错误首先出现:

def template_not_found(_template, assigns) do
  render "404.html", assigns
end
Run Code Online (Sandbox Code Playgroud)

但如果我将其更改为:

def template_not_found(_template, assigns) do
  %{message: "custom error"}
end
Run Code Online (Sandbox Code Playgroud)

它实际上并不发送 json ,而是返回 me no function clause matching in Phoenix.Template.HTML.encode_to_iodata!/1。我相信这是因为它需要发送一些 html。可以改成发送json吗?

我的路由器:

defmodule AppWeb.Router do
  use AppWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", AppWeb do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
  end

  scope …
Run Code Online (Sandbox Code Playgroud)

json elixir phoenix-framework

0
推荐指数
1
解决办法
1816
查看次数

Phoenix 1.3 闪光灯不显示

我的方法put_flash(conn)和get_flash(conn)方法表现得很奇怪;当我使用重定向时,一切正常,但当我尝试添加flash_msg后面的控制器时,什么也没有显示。

看起来消息存储在:phoenix_flash而不是:plug_session; 知道那里出了什么问题吗?

  def index(conn, _params) do
    conn
    |> put_flash(:info, "Welcome : info")
    |> put_flash(:error, "Welcome : error")
    render conn, "index.html"
  end

private: %{EverlearnWeb.Router => {[], %{}}, 
:phoenix_action => :index,
:phoenix_controller => EverlearnWeb.PageController,
:phoenix_endpoint => EverlearnWeb.Endpoint,
:phoenix_flash => %{"error" => "Welcome : error", "info" => "Welcome : info"},
:phoenix_layout => {EverlearnWeb.LayoutView, :app},
:phoenix_pipelines => [:browser], :phoenix_router => EverlearnWeb.Router,
:phoenix_view => EverlearnWeb.PageView,
:plug_session => %{"_csrf_token" => "xxx",
 "phoenix_flash" => %{"info" => …
Run Code Online (Sandbox Code Playgroud)

elixir flash-message phoenix-framework

0
推荐指数
1
解决办法
1130
查看次数

Elixir Enum.map 通过管道传输到 Enum.filter 不起作用

我正在尝试编写一个工具,该工具将从 中获取文件/文件夹列表File.ls,添加它们的完整路径,并根据它们是否是目录来过滤它们。

这有效:

directory_contents = File.ls!(directory_path)

contents_with_full_paths = Enum.map directory_contents, fn(item) -> Path.join(directory_path, item) end
only_dirs = Enum.filter contents_with_full_paths, fn(item) -> File.dir?(item) end
Run Code Online (Sandbox Code Playgroud)

这不会:

directory_contents = File.ls!(directory_path)

directory_contents
|> Enum.map fn(item) -> Path.join(directory_path, item) end
|> Enum.filter fn(item) -> File.dir?(item) end
Run Code Online (Sandbox Code Playgroud)

它抛出

** (Protocol.UndefinedError) protocol Enumerable not implemented for #Function<1.10194857/1 in RepoFinder.subdirectories_in/1>, only anonymous functions of arity 2 are enumerable. This protocol is implemented for: Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
    (elixir) …
Run Code Online (Sandbox Code Playgroud)

pipeline enumerable elixir

0
推荐指数
1
解决办法
757
查看次数

(Postgrex.Error)错误 42P18(不确定数据类型),当它尝试查询时。

我有一个查询函数,它给出utc_date_from和utc_date_to并取 的和total。

由于我需要提供本地日期,因此我将time_period本地日期添加到 中select。

time_period = to_string(date)
Payment
|> where([p], p.core_id == ^core_id and
              p.inserted_at >= datetime_add(^utc_date_from, 0, "day") and
              p.inserted_at <= datetime_add(^utc_date_to, 0, "day"))
|> select([p], %{amount: sum(p.total), time_period: ^time_period})
|> Repo.one()
Run Code Online (Sandbox Code Playgroud)

但是,如果我添加time_period到select,它会触发一个错误(Postgrex.Error) ERROR 42P18 (indeterminate_datatype): could not determine data type of parameter $1。

此外,此错误仅在我在 ubuntu 中运行我的应用程序时显示。如果我尝试使用 mac os,它不会显示此错误。

这个函数有什么问题吗?我怎样才能让它在ubuntu上工作?

postgresql elixir ecto

0
推荐指数
1
解决办法
1271
查看次数

Elixir 无法在定义它的同一文件中创建结构

我首先要说的是,我是 Elixir 世界(以及一般的函数式编程)的新手。我目前正在学习字典类型,并遇到了结构。我按照我的书的说明定义了这个:

defmodule Subscriber do
  defstruct name: "", paid: false, over_18: true
end
Run Code Online (Sandbox Code Playgroud)

之后,这本书在本章的其余部分都可以在 iex 中运行,但我希望将我的放在一个文件中。我尝试创建此结构的实例(所有值设置为默认值),如下所示:

sub = %Subscriber{}
Run Code Online (Sandbox Code Playgroud)

这个游戏给我一个编译错误:

CompileError: cannot access struct Subscriber, the struct was not yet defined or the struct is being accessed in the same context that defines it
Run Code Online (Sandbox Code Playgroud)

它在 iex 中工作得很好,但就像我说的,我宁愿把它放在一个文件中。有人可以解释我在这里做错了什么吗?

elixir

0
推荐指数
1
解决办法
906
查看次数

将列表转换为元组列表

我有一个这样的列表:

[
    ["5b71d7e458c37fa04a7ce768", "5b3f77502dfe0deeb8912b42", "1538077790705827"],
     ["5b71d7e458c37fa04a7ce768","5b3f77502dfe0deeb8912b42","1538078530667847"],
     ["5b71d7e458c37fa04a7ce768","5b3f77502dfe0deeb8912b42","1538077778390908"],
     ["5b71d7e458c37fa04a7ce768","5bad45b1e990057961313822","1538082492283531"]
]
Run Code Online (Sandbox Code Playgroud)

我想将其转换为如下列表:

[ 
  {"5b3f77502dfe0deeb8912b42",
   [{"5b71d7e458c37fa04a7ce768","5b3f77502dfe0deeb8912b42", "1538077790705827"},
    {"5b71d7e458c37fa04a7ce768","5b3f77502dfe0deeb8912b42", "1538078530667847"},
    {"5b71d7e458c37fa04a7ce768", "5b3f77502dfe0deeb8912b42" ,"1538077778390908"}
   ]},

{"5bad45b1e990057961313822",
   [{"5b71d7e458c37fa04a7ce768","5bad45b1e990057961313822","1538082492283531"}
   ]}
]
Run Code Online (Sandbox Code Playgroud)

所以关键是从原始列表中的第二项创建元组

[“5b71d7e458c37fa04a7ce768”,“5b3f77502dfe0deeb8912b42”,“1538077790705827”],

elixir

0
推荐指数
1
解决办法
3592
查看次数

在 Phoenix 路由器中使用不带 :id 的资源宏

默认情况下,宏会在路由末尾resources附加一个,用于对单个资源执行操作。/:id

但是,我遇到的情况是,用户必须始终登录才能对自己执行操作。我更喜欢:current_user从获取conn.assigns,而不是通过传入/显示 URL 中的任何参数。我知道我总能把路线一一写出来。我只是想知道是否有办法让resources生成这样的路径。

elixir phoenix-framework

0
推荐指数
1
解决办法
625
查看次数

如何自定义 Plug.Parsers JSON 解码器的选项?

使用Plug.Parsers,可以配置用于解析 JSON 请求的模块:

plug Plug.Parsers,
  parsers: [:json],
  json_decoder: Jason
Run Code Online (Sandbox Code Playgroud)

但是,使用此语法Jason.decode!/2会使用空opts参数进行调用。

怎样才能给指定的选项提供呢json_decoder?例如,我想将keys: :atoms选项传递给Jason.decode!/2.

elixir json-deserialization plug

0
推荐指数
1
解决办法
791
查看次数

elixir - 加载模块 ranch_server 时出错 - 使用 20 编译器重新编译此模块

我正在尝试在我的另一台计算机上运行一个现有的 Elixir 项目,在一台新计算机上(两台都是 mac)。

当我运行 iex -S mix 时,我得到:

iex -S mix
Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10] [hipe] [kernel-poll:false]


11:47:52.651 [error] Loading of /Users/benjamin/vepo/api/_build/dev/lib/ranch/ebin/ranch_server.beam failed: :badfile


11:47:52.651 [error] beam/beam_load.c(1863): Error loading module ranch_server:
  This BEAM file was compiled for a later version of the run-time system than 20.
  To fix this, please recompile this module with an 20 compiler.
  (Use of opcode 164; this emulator supports only up to 159.)



11:47:52.657 [info]  Application ranch exited: :ranch_app.start(:normal, []) …
Run Code Online (Sandbox Code Playgroud)

erlang elixir-mix elixir beam

0
推荐指数
1
解决办法
2740
查看次数